Created
June 1, 2015 20:30
-
-
Save r0adkll/df171d995cfd062b31d7 to your computer and use it in GitHub Desktop.
Small helper function for doing kerning on android texts
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
/** | |
* Apply kerning to a string | |
* | |
* @param src the source string | |
* @param kerning the amount of kerning | |
* @return the spannable output | |
*/ | |
public static Spannable applyKerning(CharSequence src, float kerning) { | |
if (src == null) return null; | |
final int srcLength = src.length(); | |
if (srcLength < 2) return src instanceof Spannable | |
? (Spannable) src | |
: new SpannableString(src); | |
final String nonBreakingSpace = "\u00A0"; | |
final SpannableStringBuilder builder = src instanceof SpannableStringBuilder | |
? (SpannableStringBuilder) src | |
: new SpannableStringBuilder(src); | |
for (int i = src.length() - 1; i >= 1; i--) { | |
builder.insert(i, nonBreakingSpace); | |
builder.setSpan(new ScaleXSpan(kerning), i, i + 1, | |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
} | |
return builder; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is actually spacing, not kerning.