如何获取固定宽度的文本高度并获取适合框架的文本长度?

发布于 2024-10-06 02:36:36 字数 326 浏览 2 评论 0原文

好吧,我已经设法在标题中解决了我的所有问题。我需要将长文本分成列/框架并将其布局以供查看。我已经寻找解决方案几天了,但我找不到任何示例或清晰的文档来说明如何完成我的任何任务。我看到一些关于 StaticLayout 的提及,但我不知道如何正确使用它。至于文本高度,我尝试过 TextPaint 的 getTextBounds 方法,但它没有宽度限制,而且看起来它只测量单行(好吧,也许我做错了什么)。

也许有人有 StaticLayout 或其子类用法的示例?

一切在“纸上”看起来都很简单:创建“框架”,检查其中适合多少字符,填充框架并定位它,重复直到文本结束,但我找不到任何关于如何执行此操作的信息:)

well, I've managed to fit all my questions in the title. I need to break a long text in to columns/frames and layout them in to view. I've been digging for solutions for a couple of days now, but I can't find any examples or clear documentation on how to complete any of my tasks. I've seen a some mentions of StaticLayout, but I don't know how to use it properly. As for text height I've tried TextPaint's getTextBounds method, but it doesn't have width limit and it looks like it measures only single line (well, maybe I was doing something wrong).

Maybe someone has an example of StaticLayout or it's subclass usage?

Everything looks so simple "on paper": create "frame", check how much characters fits in it, fill frame and position it, repeat until end of text and yet I can't find anything on how to do this :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-10-13 02:36:36

我不知道您问题的确切答案,但通常您可以使用图形库中提供的方法根据字体类型和大小计算文本的宽度和高度。

我已经用 C# 和 Java 完成了它。在 C# 中称为“MeasureString”,在 Java 中称为“FontMetrics”。

编辑:

看看这段代码是否有用(我没有编译它,因为我这里没有 android SDK):

    String myText="";       
    String tempStr="";

    int startIndex=0;
    int endIndex=0;

    //calculate end index that fits
    endIndex=myPaint.breakText(myTest, true, frameWidth, null)-1;   

    //substring that fits into the frame
    tempStr=myText.substring(startIndex,endIndex);

    while(endIndex < myText.length()-1)
    {           

        //draw or add tempStr to the Frame
        //at this point 

        //set new start index
        startIndex=endIndex+1;

        //substring the remaining of text
        tempStr=myText.substring(startIndex,myText.length()-1);

        //calculate end of index that fits
        endIndex=myPaint.breakText(tempStr, true, frameWidth, null)-1;  

        //substring that fits into the frame
        tempStr=myText.substring(startIndex,endIndex);
    }

Android 也有类似的方法:

breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth)

测量文本,如果测量的宽度超过 maxWidth,则提前停止。< /em>

measureText(String text)

返回文本的宽度。

http: //developer.android.com/reference/android/graphics/Paint.html

I don't know the exact answer to your question, but generally you can calculate the width and height of a text based on the font type and size using methods available in the graphical library.

I've done it in C# and Java. in C# it's called "MeasureString", and in Java, "FontMetrics".

EDIT:

See if this code is useful ( I haven't compiled it because I don't have android SDK here):

    String myText="";       
    String tempStr="";

    int startIndex=0;
    int endIndex=0;

    //calculate end index that fits
    endIndex=myPaint.breakText(myTest, true, frameWidth, null)-1;   

    //substring that fits into the frame
    tempStr=myText.substring(startIndex,endIndex);

    while(endIndex < myText.length()-1)
    {           

        //draw or add tempStr to the Frame
        //at this point 

        //set new start index
        startIndex=endIndex+1;

        //substring the remaining of text
        tempStr=myText.substring(startIndex,myText.length()-1);

        //calculate end of index that fits
        endIndex=myPaint.breakText(tempStr, true, frameWidth, null)-1;  

        //substring that fits into the frame
        tempStr=myText.substring(startIndex,endIndex);
    }

Similar methods are available for Android:

breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth)

Measure the text, stopping early if the measured width exceeds maxWidth.

measureText(String text)

Return the width of the text.

http://developer.android.com/reference/android/graphics/Paint.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文