Created
April 21, 2017 11:01
-
-
Save Popalay/a35d341faeabb663fe96a8c8d72f9e05 to your computer and use it in GitHub Desktop.
FitText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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