android 设置字体中间加间距

原理:给字与字之间添加空格,并对空格进行水平缩放
1.自定义属性
 

<!--字间距-->
<declare-styleable name="SpaceTextView">
    <attr name="text_space" format="dimension"></attr>
    <attr name="text_content" format="string"></attr>
</declare-styleable>
2.自定义控件

package com.dejun.commonsdk.wight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;
import android.widget.TextView;

import com.dejun.commonsdk.R;

/**
 * Author:DoctorWei
 * Time:2019/1/10 19:10
 * Description:自定义TextView解决中文字换行和中文字问题
 * email:1348172474@qq.com
 */

@SuppressLint("AppCompatCustomView")
public class SpaceTextView extends TextView{
    private float spacing = Spacing.NORMAL;
    private CharSequence originalText = "";
    public SpaceTextView(Context context) {
        super(context);
    }

    public SpaceTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.SpaceTextView);
        float textSize = typedArray.getDimension(R.styleable.SpaceTextView_text_space, 0f);
        String textContent = typedArray.getString(R.styleable.SpaceTextView_text_content);
        setSpacing(textSize);
        setText(textContent);
        typedArray.recycle();
    }

    public SpaceTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 获取字间距
     */
    public float getSpacing() {
        return this.spacing;
    }

    /**
     * 设置间距
     */
    public void setSpacing(float spacing) {
        this.spacing = spacing;
        applySpacing();
    }

    @Override
    public void setText(CharSequence text, TextView.BufferType type) {
        originalText = text;
        applySpacing();
    }

    @Override
    public CharSequence getText() {
        return originalText;
    }

    /**
     * 扩大文字空间
     */
    private void applySpacing() {
        if (this == null || this.originalText == null) return;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < originalText.length(); i++) {
            builder.append(originalText.charAt(i));
            if (i + 1 < originalText.length()) {
                // \u00A0 不间断空格 碰见文字追加空格
                if (isEnglish(originalText.charAt(i) + "") && isEnglish(originalText.charAt(i + 1) + "")) {
                    builder.append("\u00A0");
                } else {
                    builder.append("\u00A0");
                }
            }
        }
        // 通过SpannableString类,去设置空格
        SpannableString finalText = new SpannableString(builder.toString());
        // 如果当前TextView内容长度大于1,则进行空格添加
        if (builder.toString().length() > 1) {
            for (int i = 1; i < builder.toString().length(); i += 2) {
                // ScaleXSpan 基于x轴缩放  按照x轴等比例进行缩放 通过字间距+1除以10进行等比缩放
                finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        super.setText(finalText, TextView.BufferType.SPANNABLE);
    }

    public class Spacing {
        public final static float NORMAL = 0;
    }
    /**
     *  判断是否是英语
     */
    public static boolean isEnglish(String charaString) {
        return charaString.matches("^[a-zA-Z]*");
    }
}

3.使用
 

<com.dejun.passionet.commonsdk.widget.SpaceTextView
    android:id="@+id/passionet_start_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="140dp"
    android:gravity="center"
    app:text_space="1dp"
    app:text_content="PASSIONET"
    android:textColor="@color/trans_white_80"
    android:textSize="24sp" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值