Closed jerryafr closed 9 years ago
Merging into #5
I implemented this option myself before finding it on here. The below is a slightly optimized version that scales the height using ratio instead. I think it would be a good idea to include a bool member (such as the mHelper.mEnabled bool) that would control height scaling in the library. (I can do this in some spare time if you like)
int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingTop();
if (targetHeight <= 0) {
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
return;
}
float textHeight = getTextHeight(text, paint, targetWidth, size);
textHeight = getTextHeight(text, paint, targetWidth, size);
float heightRatio = targetHeight / textHeight;
float newSize = size * heightRatio;
if (newSize < size) {
size = newSize;
}
Text height is not measured and considered. So, when a single line short text (like HELLO) with big textview width and small textview height is used, the size will be too big for the height.
The following method can be used to determine the height.
At the end of "void refitText()" method, using the following code would solve the problem: