如何获得文本固定宽度的高度和获得嵌合的帧文本长度? [英] How to get height of text with fixed width and get text length which fits in a frame?

查看:187
本文介绍了如何获得文本固定宽度的高度和获得嵌合的帧文本长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我已经成功地适合我的标题的所有问题。我需要在打破长的文本列/帧,布局他们查看。我已经挖了几天的解决方案,但我无法找到如何完成我的任何任务,任何实例或明确的文件。我已经看到了一些提到StaticLayout的,但我不知道如何正确使用它。至于文字高度我试过TextPaint的getTextBounds方法,但它没有宽度的限制,它看起来像它的措施只是单行(好吧,也许我是做错了什么)。

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).

也许有人StaticLayout的例子,或者它的子类的用法?

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 :)

推荐答案

我不知道你的问题的确切答案,但一般可以计算文本的基础上,字体类型和使用方法大小的宽度和高度可在图形库。

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.

我在C#和Java做了。在C#中,它被称为MeasureString,并在Java中,的FontMetrics。

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

编辑:

看是否有此code是有用的(我没有编译它,因为我没有在这里的Andr​​oid SDK):

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);
    }

类似的方法可用于Android:

Similar methods are available for Android:

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

测量文本,停止早期如果测量的宽度超过了maxWidth。

measureText(String text)

返回文本的宽度。

<一个href="http://developer.android.com/reference/android/graphics/Paint.html">http://developer.android.com/reference/android/graphics/Paint.html

这篇关于如何获得文本固定宽度的高度和获得嵌合的帧文本长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆