110

如果可能的话,我也想获得身高。

4

7 回答 7

213

您可以使用getTextBounds(String text, int start, int end, Rect bounds)Paint 对象的方法。您可以使用 a 提供的绘制对象,也可以TextView自己构建一个具有所需文本外观的对象。

使用 Textview 您可以执行以下操作:

Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.height();
int width = bounds.width();
于 2010-09-02T19:05:39.743 回答
87

如果您只需要宽度,您可以使用:

float width = paint.measureText(string);

http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String)

于 2012-06-17T00:55:57.113 回答
21

文本有两种不同的宽度度量。一个是在宽度上绘制的像素数,另一个是在绘制文本后光标应该前进的“像素”数。

paint.measureTextpaint.getTextWidths返回在绘制给定字符串后光标应该前进的像素数(浮点数)。对于绘制的像素数,请使用其他答案中提到的paint.getTextBounds。我相信这被称为字体的“前进”。

对于某些字体,这两个测量值不同(很多),例如字体Black Chancery的字母超出其他字母(重叠) - 请参见大写字母“L”。使用其他答案中提到的paint.getTextBounds来绘制像素。

于 2013-08-13T09:14:26.123 回答
6

我以这种方式测量了宽度:

String str ="Hiren Patel";

Paint paint = new Paint();
paint.setTextSize(20);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(str, 0, str.length(), result);

Log.i("Text dimensions", "Width: "+result.width());

这会对你有所帮助。

于 2016-12-09T11:07:15.390 回答
5

很可能您想知道给定字体的给定文本字符串的绘制尺寸(即特定字体Typeface,例如具有 BOLD_ITALIC 样式的“sans-serif”字体系列,以及以 sp 或 px 为单位的特定大小)。

TextView您可以降低级别并Paint直接使用对象处理单行文本,而不是膨胀一个完整的,例如:

// Maybe you want to construct a (possibly static) member for repeated computations
Paint paint = new Paint();

// You can load a font family from an asset, and then pick a specific style:
//Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); 
//Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD);
// Or just reference a system font:
paint.setTypeface(Typeface.create("sans-serif",Typeface.BOLD));

// Don't forget to specify your target font size. You can load from a resource:
//float scaledSizeInPixels = context.getResources().getDimensionPixelSize(R.dimen.mediumFontSize);
// Or just compute it fully in code:
int spSize = 18;
float scaledSizeInPixels = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            spSize,
            context.getResources().getDisplayMetrics());
paint.setTextSize(scaledSizeInPixels);

// Now compute!
Rect bounds = new Rect();
String myString = "Some string to measure";
paint.getTextBounds(myString, 0, myString.length(), bounds);
Log.d(TAG, "width: " + bounds.width() + " height: " + bounds.height());



对于多行或跨行文本( SpannedString),请考虑使用 a StaticLayout,您可以在其中提供宽度并导出高度。有关在自定义视图中测量和绘制文本到画布的非常详细的答案,请参阅:https ://stackoverflow.com/a/41779935/954643

同样值得注意的是@arberg在下面关于绘制的像素与提前宽度的回复(“在绘制给定字符串后光标应该前进的像素数(浮点数)”),以防您需要处理。

于 2018-08-05T09:44:47.180 回答
4

我想分享一种更好的方法(比当前接受的答案更通用String)使用静态类获取绘制文本的确切宽度( ) StaticLayout

StaticLayout.getDesiredWidth(text, textPaint))

此方法比 更准确textView.getTextBounds(),因为您可以计算 multiline 中单行的宽度TextView,或者您可能不使用TextView开头(例如在自定义View实现中)。

这种方式类似于textPaint.measureText(text),但在极少数情况下似乎更准确。

于 2019-06-06T16:32:09.550 回答
-1

simplay 我在行中添加最大字符并用最大空间挑战它并创建新行

    v_y = v_y + 30;
    String tx = "مبلغ وقدرة : "+ AmountChar+" لا غير";

    myPaint.setTextAlign(Paint.Align.RIGHT);

    int pxx = 400;
    int pxy = v_y ;
    int word_no = 1;
    int word_lng = 0;
    int max_word_lng = 45;
    int new_line = 0;
    int txt_lng = tx.length();
    int words_lng =0;
    String word_in_line = "" ;
    for (String line : tx.split(" "))
    {
        word_lng = line.length() ;
        words_lng += line.length() + 1;

        if (word_no == 1 )
        {word_in_line = line;
        word_no += 1;
        }
        else
            { word_in_line += " " + line;
            word_no += 1;
            }

        if (word_in_line.length() >= max_word_lng)
       {
           canvas.drawText(word_in_line, pxx, pxy, myPaint);
            new_line += 1;
            pxy = pxy + 30;
            word_no = 1;
            word_in_line = "";
        }
        if (txt_lng <= words_lng )
        { canvas.drawText(word_in_line, pxx, pxy, myPaint); }
    }

    v_y = pxy;
于 2021-04-21T14:03:52.010 回答