in app/src/main/java/com/google/reviewit/widget/MaxFontSizeTextView.java [103:146]
private void refitText(CharSequence text, int textWidth) {
if (textWidth <= 0) {
return;
}
int width = textWidth - getPaddingLeft() - getPaddingRight();
paint.set(getPaint());
// TODO works only for monospace font
String longestLine = null;
for (String line : text.toString().split("\n")) {
if (longestLine != null && line.length() <= longestLine.length()) {
continue;
}
longestLine = line;
}
if (longestLine == null) {
return;
}
if (Float.valueOf(minTextSize).equals(maxTextSize)) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize);
return;
}
if (maxLineLength > 0 && longestLine.length() > maxLineLength) {
longestLine = longestLine.substring(0, maxLineLength);
}
float high = maxTextSize;
float low = minTextSize;
while ((high - low) > THRESHOLD) {
float size = (high + low) / 2;
paint.setTextSize(size);
if (paint.measureText(longestLine) >= width) {
// font size too big
high = size;
} else {
// font size too small
low = size;
}
}
setTextSize(TypedValue.COMPLEX_UNIT_PX, low);
}