设置字间距


1、直接设置
  1. public static Spannable applyKerning(CharSequence src, float kerning, int start, int end){
  2. if (src == null)
  3. return null;
  4. final int srcLength = src.length();
  5. if (srcLength < 2)
  6. return src instanceof Spannable ? (Spannable)src: new SpannableString(src);
  7. if (start < 0)
  8. start = 0;
  9. if (end > srcLength)
  10. end = srcLength;
  11. final String nonBreakingSpace = "\u00A0";
  12. final SpannableStringBuilder builder = src instanceof SpannableStringBuilder ? (SpannableStringBuilder)src: new SpannableStringBuilder(src);
  13. for (int i = src.length(); i >= 1; i--) {
  14. builder.insert(i, nonBreakingSpace);
  15. builder.setSpan(new ScaleXSpan(kerning), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  16. }
  17. return builder;
  18. }

2、自定义控件
  1. /**
  2. * Text view that allows changing the letter spacing of the text.
  3. *
  4. * @author Pedro Barros (pedrobarros.dev at gmail.com)
  5. * @since May 7, 2013
  6. */
  7. import android.content.Context;
  8. import android.text.Spannable;
  9. import android.text.SpannableString;
  10. import android.text.style.ScaleXSpan;
  11. import android.util.AttributeSet;
  12. import android.widget.TextView;
  13. public class LetterSpacingTextView extends TextView {
  14. private float letterSpacing = LetterSpacing.NORMAL;
  15. private CharSequence originalText = "";
  16. public LetterSpacingTextView(Context context) {
  17. super(context);
  18. }
  19. public LetterSpacingTextView(Context context, AttributeSet attrs){
  20. super(context, attrs);
  21. }
  22. public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
  23. super(context, attrs, defStyle);
  24. }
  25. public float getLetterSpacing() {
  26. return letterSpacing;
  27. }
  28. public void setLetterSpacing(float letterSpacing) {
  29. this.letterSpacing = letterSpacing;
  30. applyLetterSpacing();
  31. }
  32. @Override
  33. public void setText(CharSequence text, BufferType type) {
  34. originalText = text;
  35. applyLetterSpacing();
  36. }
  37. @Override
  38. public CharSequence getText() {
  39. return originalText;
  40. }
  41. private void applyLetterSpacing() {
  42. StringBuilder builder = new StringBuilder();
  43. for(int i = 0; i < originalText.length(); i++) {
  44. builder.append(originalText.charAt(i));
  45. if(i+1 < originalText.length()) {
  46. builder.append("\u00A0");
  47. }
  48. }
  49. SpannableString finalText = new SpannableString(builder.toString());
  50. if(builder.toString().length() > 1) {
  51. for(int i = 1; i < builder.toString().length(); i+=2) {
  52. finalText.setSpan(new ScaleXSpan((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  53. }
  54. }
  55. super.setText(finalText, BufferType.SPANNABLE);
  56. }
  57. public class LetterSpacing {
  58. public final static float NORMAL = 0;
  59. }
  60. }

调用方法:
  1. LetterSpacingTextView textView = new LetterSpacingTextView(context);
  2. textView.setLetterSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.LetterSpacing.NORMAL
  3. textView.setText("My text");
  4. //Add the textView in a layout, for instance:
  5. ((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);





posted @ 2016-01-04 13:01  风倾清凌  阅读(855)  评论(0编辑  收藏  举报