Reputation: 532
I have the following code to find the screen dimensions calculate how much text fits on the screen.
Now I need to know how many lines fit on the screen. I have the size of the text from the ascent to the descent. I need to know the height of the line spacing. getFontSpacing also gives the value from the ascent to the descent.
Does anyone know if there is a way of finding the line spacing's value?
//Code for Getting screen Dimensions
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//Breaking Text When Width Is Filled
Paint p = new Paint();
p.setTextSize(60);
int textNum = p.breakText(sText, 0, 20, forOrBack, width, null);
//Working Out How Many Lines Can Be Entered In The Screen
float fHeight = p.descent() - p.ascent();
int tHeight = (int) fHeight;
int numLines = height/tHeight;
Upvotes: 3
Views: 5532
Reputation: 300
For line spacing use textView.getLineHeight();
reference : http://developer.android.com/reference/android/widget/TextView.html#getLineHeight()
Upvotes: 3
Reputation: 3961
The following will return the recommended line spacing
textView.getPaint().getFontSpacing()
you can get the height of you text several ways. I prefer paint's getTextBounds method and just add the two values together
Upvotes: 3
Reputation: 3964
Paint.FontMetrics fm = paint.getFontMetrics();
float fullHeight = fm.top - fm.bottom;
As far as I remember, fm.bottom < 0, therefore by subtracting you actually get
Math.abs(fm.top) + Math.abs(fm.bottom)
This value is bigger than ascent-descent, and, I guess, it is the actual line size.
You may consider using TextView.getLineHeight() after setting TextSize and preferebly after layout is complete (e.g in OnWindowFocusChanged).
Upvotes: 2