Skip to content

Instantly share code, notes, and snippets.

@r0adkll
Created June 1, 2015 20:30
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 r0adkll/df171d995cfd062b31d7 to your computer and use it in GitHub Desktop.
Save r0adkll/df171d995cfd062b31d7 to your computer and use it in GitHub Desktop.
Small helper function for doing kerning on android texts
/**
* 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;
}
@ASemeniuk
Copy link

This is actually spacing, not kerning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment