小智 195

您可以使用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();
Run Code Online (Sandbox Code Playgroud)

  • @Frank:如果String是跨区字符串怎么办? (5认同)

Sha*_*ley 73

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

float width = paint.measureText(string);
Run Code Online (Sandbox Code Playgroud)

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

  • 另外,如果要测量特定字体大小的文本,可以使用`TextPaint paint = new TextPaint(); paint.setTextSize(TEXTSIZE);`.`textSize`以像素为单位. (3认同)

arb*_*erg 19

文本有两种不同的宽度度量.一个是宽度绘制的像素数,另一个是绘制文本后光标应该提前的"像素数".

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

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


Hir*_*tel 5

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

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());
Run Code Online (Sandbox Code Playgroud)

这会对你有所帮助.


qix*_*qix 5

您很可能想知道具有给定字体的给定文本字符串的绘制尺寸(即特定的Typeface\xe2\x80\x9csans-serif\xe2\x80\x9d 字体系列,具有 BOLD_ITALIC 样式和特定大小以 sp 或 px 为单位)。

\n\n

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

\n\n
// Maybe you want to construct a (possibly static) member for repeated computations\nPaint paint = new Paint();\n\n// You can load a font family from an asset, and then pick a specific style:\n//Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); \n//Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD);\n// Or just reference a system font:\npaint.setTypeface(Typeface.create("sans-serif",Typeface.BOLD));\n\n// Don\'t forget to specify your target font size. You can load from a resource:\n//float scaledSizeInPixels = context.getResources().getDimensionPixelSize(R.dimen.mediumFontSize);\n// Or just compute it fully in code:\nint spSize = 18;\nfloat scaledSizeInPixels = TypedValue.applyDimension(\n            TypedValue.COMPLEX_UNIT_SP,\n            spSize,\n            context.getResources().getDisplayMetrics());\npaint.setTextSize(scaledSizeInPixels);\n\n// Now compute!\nRect bounds = new Rect();\nString myString = "Some string to measure";\npaint.getTextBounds(myString, 0, myString.length(), bounds);\nLog.d(TAG, "width: " + bounds.width() + " height: " + bounds.height());\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

另外值得注意的是@arberg下面关于绘制的像素与前进宽度的回复(“绘制给定字符串后光标应前进的像素数(以浮点数表示)”),以防您需要处理这个问题。

\n


Ser*_*vic 5

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

StaticLayout.getDesiredWidth(text, textPaint))
Run Code Online (Sandbox Code Playgroud)

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

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