How to change letter spacing in a Textview?

后端 未结 8 1779
温柔的废话
温柔的废话 2020-11-29 18:30

How can i change letter spacing in a textview? Will it help if I have HTML text in it (I cannot use webview in my code).

P.S. I\'m using my own typeface in the textv

相关标签:
8条回答
  • 2020-11-29 19:09

    More space:

      android:letterSpacing="0.1"
    

    Less space:

     android:letterSpacing="-0.07"
    
    0 讨论(0)
  • 2020-11-29 19:11

    Since API 21 there is an option set letter spacing. You can call method setLetterSpacing or set it in XML with attribute letterSpacing.

    0 讨论(0)
  • 2020-11-29 19:13

    For embedding HTML text in your textview you can use Html.fromHTML() syntax. More information you will get from http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29

    0 讨论(0)
  • 2020-11-29 19:17

    This answer is based on Pedro's answer but adjusted so it also works if text attribute is already set:

    package nl.raakict.android.spc.widget;
    import android.content.Context;
    import android.text.Spannable;
    import android.text.SpannableString;
    import android.text.style.ScaleXSpan;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    
    public class LetterSpacingTextView extends TextView {
        private float letterSpacing = LetterSpacing.BIGGEST;
        private CharSequence originalText = "";
    
    
        public LetterSpacingTextView(Context context) {
            super(context);
        }
    
        public LetterSpacingTextView(Context context, AttributeSet attrs){
            super(context, attrs);
            originalText = super.getText();
            applyLetterSpacing();
            this.invalidate();
        }
    
        public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
            super(context, attrs, defStyle);
        }
    
        public float getLetterSpacing() {
            return letterSpacing;
        }
    
        public void setLetterSpacing(float letterSpacing) {
            this.letterSpacing = letterSpacing;
            applyLetterSpacing();
        }
    
        @Override
        public void setText(CharSequence text, BufferType type) {
            originalText = text;
            applyLetterSpacing();
        }
    
        @Override
        public CharSequence getText() {
            return originalText;
        }
    
        private void applyLetterSpacing() {
            if (this == null || this.originalText == null) return;
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < originalText.length(); i++) {
                String c = ""+ originalText.charAt(i);
                builder.append(c.toLowerCase());
                if(i+1 < originalText.length()) {
                    builder.append("\u00A0");
                }
            }
            SpannableString finalText = new SpannableString(builder.toString());
            if(builder.toString().length() > 1) {
                for(int i = 1; i < builder.toString().length(); i+=2) {
                    finalText.setSpan(new ScaleXSpan((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            super.setText(finalText, BufferType.SPANNABLE);
        }
    
        public class LetterSpacing {
            public final static float NORMAL = 0;
            public final static float NORMALBIG = (float)0.025;
            public final static float BIG = (float)0.05;
            public final static float BIGGEST = (float)0.2;
        }
    }
    

    If you want to use it programatically:

    LetterSpacingTextView textView = new LetterSpacingTextView(context);
    textView.setSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL
    textView.setText("My text");
    //Add the textView in a layout, for instance:
    ((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);
    
    0 讨论(0)
  • 2020-11-29 19:20

    I built a custom class that extends TextView and solves this problem... Check out my answer here =)

    0 讨论(0)
  • 2020-11-29 19:21

    As android doesn't support such a thing, you can do it manually with FontCreator. It has good options for font modifying. I used this tool to build a custom font, even if it takes some times but you can always use it in your projects.

    0 讨论(0)
提交回复
热议问题