您的位置:首页 > 其它

设置字间距

2016-01-04 13:01 176 查看
来源:http://stackoverflow.com/questions/1640659/how-to-adjust-text-kerning-in-android-textview/16429758#16429758
1、直接设置
public static Spannable applyKerning(CharSequence src, float kerning, int start, int end){

if (src == null)

return null;

final int srcLength = src.length();

if (srcLength < 2)

return src instanceof Spannable ? (Spannable)src: new SpannableString(src);


if (start < 0)

start = 0;

if (end > srcLength)

end = srcLength;


final String nonBreakingSpace = "\u00A0";

final SpannableStringBuilder builder = src instanceof SpannableStringBuilder ? (SpannableStringBuilder)src: new SpannableStringBuilder(src);


    for (int i = src.length(); i >= 1; i--) {  

   builder.insert(i, nonBreakingSpace);  

   builder.setSpan(new ScaleXSpan(kerning), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}


return builder;

}

[/code]
2、自定义控件
/**

* Text view that allows changing the letter spacing of the text.

*

*@author Pedro Barros (pedrobarros.dev at gmail.com)

*@since May 7, 2013

*/

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.NORMAL;

private CharSequence originalText = "";



public LetterSpacingTextView(Context context) {

   super(context);

}


public LetterSpacingTextView(Context context, AttributeSet attrs){

   super(context, attrs);

}


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() {

   StringBuilder builder = new StringBuilder();

   for(int i = 0; i < originalText.length(); i++) {

  builder.append(originalText.charAt(i));

  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;

}

}

[/code]
调用方法:
LetterSpacingTextView textView = new LetterSpacingTextView(context);

textView.setLetterSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.LetterSpacing.NORMAL

textView.setText("My text");

//Add the textView in a layout, for instance:

((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);

[/code]

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: