美文网首页
android 富文本

android 富文本

作者: 风___________ | 来源:发表于2018-08-23 18:58 被阅读35次

封装一个工具类

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.example.jing.maya_android.application.KapApplication;

/**
 * Created by jing on 2017/6/16.
 * 富文本生成器
 */
public class KapAttributedStringHelper {
    /** 改变选中字体的颜色 */
    public static SpannableStringBuilder AttributedStringByConfi(String allString
            , int normalColor, int selectedColor, String[] selectedStrings){
        if (allString.length() == 0) return null;
        SpannableStringBuilder sb = new SpannableStringBuilder(allString);
        ForegroundColorSpan normalColorSpan = new ForegroundColorSpan(KapApplication.getContext().getResources().getColor(normalColor));
        ForegroundColorSpan selectedColorSpan = new ForegroundColorSpan(KapApplication.getContext().getResources().getColor(selectedColor));
        sb.setSpan(normalColorSpan, 0, allString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        for(int i=0;i<selectedStrings.length;i++){
            String selectedString = selectedStrings[i];
            int startLocation = allString.indexOf(selectedString);
            int endLocation = startLocation+selectedString.length();
            sb.setSpan(selectedColorSpan, startLocation, endLocation, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return sb;
    }
    /** 链式语法 */
    private Context context;
    private SpannableStringBuilder builder;
    private String allString;
    private String[] selectedStrings;
    private KapAttributedStringHelper(){super();}
    public KapAttributedStringHelper(Context context, String allString, String[] selectedStrings) {
        super();
        this.context = context;
        this.allString = allString;
        this.selectedStrings = selectedStrings;
        builder = new SpannableStringBuilder(allString);
    }
    public SpannableStringBuilder builder(){
        return builder;
    }
    /** 所有的字体颜色 */
    public KapAttributedStringHelper setNormalColor(int color){
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(color));
        builder.setSpan(colorSpan, 0, allString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }
    /** 选中的字体颜色 */
    public KapAttributedStringHelper setSelectlColor(int[] colors){
        int lenth = Math.min(selectedStrings.length,colors.length);
        for(int i=0; i < lenth; i++){
            String selectedString = selectedStrings[i];
            int color = colors[i];
            ForegroundColorSpan span = new ForegroundColorSpan(context.getResources().getColor(color));
            int startLocation = allString.indexOf(selectedString);
            int endLocation = startLocation+selectedString.length();
            builder.setSpan(span, startLocation, endLocation, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return this;
    }
    /** 所有的字体大小 */
    public KapAttributedStringHelper setNormalSize(int size){
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(size);
        builder.setSpan(sizeSpan, 0, allString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }
    /** 选中的字体大小 */
    public KapAttributedStringHelper setSelectlSize(int[] sizes){
        int lenth = Math.min(selectedStrings.length,sizes.length);
        for(int i=0; i < lenth; i++){
            String selectedString = selectedStrings[i];
            int size = sizes[i];
            AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(size);
            int startLocation = allString.indexOf(selectedString);
            int endLocation = startLocation+selectedString.length();
            builder.setSpan(sizeSpan, startLocation, endLocation, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return this;
    }
}

使用如下:

 {
            String showString = String.format(priceshow_price_format,minPrice);
            String[] selectString = {minPrice};
            int[] selectSize = {KapDbPxTransHelper.sp2px(this,16)};
            SpannableStringBuilder priceSpan = new KapAttributedStringHelper(this,showString,selectString)
                    .setNormalSize(KapDbPxTransHelper.sp2px(this,12))
                    .setSelectlSize(selectSize)
                    .builder();
            priceTextview.setText(priceSpan);
        }
        {
            String showString = String.format(priceshow_price_sub_prompt,minPrice,maxPrice);
            String[] selectString = {minPrice,maxPrice};
            int[] selectColor = {R.color.BANK_GRAY_525252,R.color.Bank_Yellow_ff9100};
            SpannableStringBuilder priceSpan = new KapAttributedStringHelper(this,showString,selectString)
                    .setNormalColor(R.color.BANK_GRAY_bfbfbf)
                    .setSelectlColor(selectColor)
                    .builder();
            priceSubPromptTextview.setText(priceSpan);
        }

相关文章

网友评论

      本文标题:android 富文本

      本文链接:https://www.haomeiwen.com/subject/imimiftx.html