Skip to content

Instantly share code, notes, and snippets.

@Popalay
Created April 21, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Popalay/a35d341faeabb663fe96a8c8d72f9e05 to your computer and use it in GitHub Desktop.
Save Popalay/a35d341faeabb663fe96a8c8d72f9e05 to your computer and use it in GitHub Desktop.
FitText
private void refitText(String text, int textWidth) {
if (textWidth <= 0) {
return;
}
final int targetWidth = textWidth - getPaddingLeft() - getPaddingRight();
float hi = 1f;
float lo = 0f;
float textWidthCalculated;
while (hi - lo > 0.1) {
final float size = (hi + lo) / 2;
mTestPaint.setLetterSpacing(size);
textWidthCalculated = mTestPaint.measureText(text, 0, text.length());
if (textWidthCalculated >= targetWidth) {
hi = size;
} else {
lo = size;
}
}
setLetterSpacing(lo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment