How to measure TextView height based on device width and font size?

I am looking for a method in Android that will accept input (text, text_font_size, device_width), and based on these calculations, it will return how much height will it take to display specific text?

I set the time for viewing the text view / web view based on its contents, I know about the contents of warp, but I can not use it in my case due to some problem with the minimum height of the web view.

So, I'm trying to calculate the height and based on this I set the height of the view.

I tried the following methods:

Paint paint = new Paint(); paint.setTextSize(text.length()); Rect bounds = new Rect(); paint.getTextBounds(text, 0, 1, bounds); mTextViewHeight= bounds.height(); 

Thus, the conclusion

1) "Hello World" returns a height of 13 for font 15

2) "The latest version of Jelly Bean here, with performance optimization" returns a height of 16 for font 15

Then i tried with

 Paint paint = new Paint(); paint.setTextSize(15); paint.setTypeface(Typeface.SANS_SERIF); paint.setColor(Color.BLACK); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), result); Paint.FontMetrics metrics = brush.getFontMetrics(); int totalHeight = (int) (metrics.descent - metrics.ascent + metrics.leading); 

Thus, the conclusion

1) "Hello World" returns a height of 17 for font 15

2) "The latest version of Jelly Bean here, with performance optimization" returns a height of 17 for font 15

if I set these values ​​for my view and then cut off some text without showing all the content.

Again it looks normal on some tables, as it has a large width, but not on the phone.

Is there any way to calculate height by content?

+21
android android-layout android-textview android-canvas android-view
Jan 11 '13 at 11:07
source share
3 answers
 public static int getHeight(Context context, String text, int textSize, int deviceWidth) { TextView textView = new TextView(context); textView.setText(text); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); return textView.getMeasuredHeight(); } 

If textSize not specified in pixels, change the first setTextSize() parameter.

+43
Jan 11 '13 at
source share

Paint.getTextBounds() returns not what you expect. Details here .

Instead, you can try as follows:

 int mMeasuredHeight = (new StaticLayout(mMeasuredText, mPaint, targetWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true)).getHeight(); 
+7
Mar 13 '13 at 13:16
source share

I have an easier way to find out the real line height before drawing it, I don’t know if this will help you guys, but my solution is to get the line height, regardless of the height of the layout, just take font metrics like this :

 myTextView.getPaint().getFontMetrics().bottom - myTextView.getPaint().getFontMetrics().top) 

so that we get the actual height that the fonts from the text box will use to draw. This does not give you an int, but you can do Math.round to get almost value.

+5
Oct 19 '16 at 13:48
source share



All Articles