开发者

Android and calculating the size of a one-line string in a given font and font size?

Is there an API method for calculating the size (i.e. width and height) of a str开发者_StackOverflow中文版ing displayed on one line and in a given font and font size?


Paint p = new Paint();
p.setTypeface(TypeFace obj); // if custom font use `TypeFace.createFromFile`
p.setTextSize(float size);
float textWidth = p.measureText("Your string"); 
// you get the width here and textsize is the height.


Paint mPaint = new Paint();
mPaint.setTextSize(/*put here ur size*/);
mPaint.setTypeface(/* put here ur font type */);
Rect bounds = new Rect();
mPaint.getTextBounds(text, 0, text.length(), bounds);

then make the call of

bounds.width();

bounds.height();


The Paint class has a method measureText() which will give you the width in float

Paint p = new Paint();
int INFO_WINDOW_WIDTH = (int)p.measureText("this is string");

and another is Paint.getTextBounds

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜