Reputation: 8401
I am writing an app, in which part of it displays a line of text. there are certain scenarios where that line of text will take up more than one line. the problem is that I only want it to take up one line, and when I set it up either in java or in xml to only take up one line, the text is cut off. how would I make it so that it automatically adjusts the font size of the text so that it will only take up one line without being cut off?
Upvotes: 0
Views: 2148
Reputation: 22240
Use proportions along with Paint.measureText():
(text size / measureText width) = (perfectSize / screenWidth)
Solving for the perfect text size:
perfectSize = (text size / measureText width) * screenWidth;
You can find the screen width with getWindowManager().getDefaultDisplay().getWidth()
from the Display class.
Turned it into a math problem!
Upvotes: 3
Reputation: 44919
This isn't too hard to do if you use Paint#measureText.
Basically you would start with a font height smaller than the height of your TextView
and then iterate (or do a binary search) through fonts of varying sizes until you find one that measures to smaller than the width of your TextView
.
It requires some implementation, as there is no "automatic" way provided by the framework.
Upvotes: 0