转:工具类之SpannableStringUtils(相信你会爱上它)

这个工具类真是构思了良久才设计出来,采用了建造者模式,然后你们就可以用链式调用了,talk is cheap, let me show the demo.

转:工具类之SpannableStringUtils(相信你会爱上它)_第1张图片
demo
转:工具类之SpannableStringUtils(相信你会爱上它)_第2张图片
code

有没有心动一下哈,下面就让老司机为你们开路。

站点

  • SpannableString相关工具类→SpannableStringUtils.java→Demo
    getBuilder        : 获取建造者
    setFlag           : 设置标识
    setForegroundColor: 设置前景色
    setBackgroundColor: 设置背景色
    setQuoteColor     : 设置引用线的颜色
    setLeadingMargin : 设置缩进 setBullet : 设置列表标记 setProportion : 设置字体比例 setXProportion : 设置字体横向比例 setStrikethrough : 设置删除线 setUnderline : 设置下划线 setSuperscript : 设置上标 setSubscript : 设置下标 setBold : 设置粗体 setItalic : 设置斜体 setBoldItalic : 设置粗斜体 setFontFamily : 设置字体 setAlign : 设置对齐 setBitmap : 设置图片 setDrawable : 设置图片 setUri : 设置图片 setResourceId : 设置图片 setClickSpan : 设置点击事件 setUrl : 设置超链接 setBlur : 设置模糊 append : 追加样式字符串 create : 创建样式字符串

具体路线

package com.blankj.utilcode.utils;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.net.Uri; import android.support.annotation.ColorInt; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.Layout.Alignment; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.AlignmentSpan; import android.text.style.BackgroundColorSpan; import android.text.style.BulletSpan; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.ImageSpan; import android.text.style.LeadingMarginSpan; import android.text.style.MaskFilterSpan; import android.text.style.QuoteSpan; import android.text.style.RelativeSizeSpan; import android.text.style.ScaleXSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.SubscriptSpan; import android.text.style.SuperscriptSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; import static android.graphics.BlurMaskFilter.Blur; /** * 
 * author: Blankj * blog : http://blankj.com * time : 16/12/13 * desc : SpannableString相关工具类 * 
*/ public class SpannableStringUtils { private SpannableStringUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 获取建造者 * * @return {@link Builder} */ public static Builder getBuilder(@NonNull CharSequence text) { return new Builder(text); } public static class Builder { private int defaultValue = 0x12000000; private CharSequence text; private int flag; @ColorInt private int foregroundColor; @ColorInt private int backgroundColor; @ColorInt private int quoteColor; private boolean isLeadingMargin; private int first; private int rest; private boolean isBullet; private int gapWidth; private int bulletColor; private float proportion; private float xProportion; private boolean isStrikethrough; private boolean isUnderline; private boolean isSuperscript; private boolean isSubscript; private boolean isBold; private boolean isItalic; private boolean isBoldItalic; private String fontFamily; private Alignment align; private boolean imageIsBitmap; private Bitmap bitmap; private boolean imageIsDrawable; private Drawable drawable; private boolean imageIsUri; private Uri uri; private boolean imageIsResourceId; @DrawableRes private int resourceId; private ClickableSpan clickSpan; private String url; private boolean isBlur; private float radius; private Blur style; private SpannableStringBuilder mBuilder; private Builder(@NonNull CharSequence text) { this.text = text; flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; foregroundColor = defaultValue; backgroundColor = defaultValue; quoteColor = defaultValue; proportion = -1; xProportion = -1; mBuilder = new SpannableStringBuilder(); } /** * 设置标识 * * @param flag
    *
  • {@link Spanned#SPAN_INCLUSIVE_EXCLUSIVE}
  • *
  • {@link Spanned#SPAN_INCLUSIVE_INCLUSIVE}
  • *
  • {@link Spanned#SPAN_EXCLUSIVE_EXCLUSIVE}
  • *
  • {@link Spanned#SPAN_EXCLUSIVE_INCLUSIVE}
  • *
* @return {@link Builder} */ public Builder setFlag(int flag) { this.flag = flag; return this; } /** * 设置前景色 * * @param color 前景色 * @return {@link Builder} */ public Builder setForegroundColor(@ColorInt int color) { this.foregroundColor = color; return this; } /** * 设置背景色 * * @param color 背景色 * @return {@link Builder} */ public Builder setBackgroundColor(@ColorInt int color) { this.backgroundColor = color; return this; } /** * 设置引用线的颜色 * * @param color 引用线的颜色 * @return {@link Builder} */ public Builder setQuoteColor(@ColorInt int color) { this.quoteColor = color; return this; } /** * 设置缩进 * * @param first 首行缩进 * @param rest 剩余行缩进 * @return {@link Builder} */ public Builder setLeadingMargin(int first, int rest) { this.first = first; this.rest = rest; isLeadingMargin = true; return this; } /** * 设置列表标记 * * @param gapWidth 列表标记和文字间距离 * @param color 列表标记的颜色 * @return {@link Builder} */ public Builder setBullet(int gapWidth, int color) { this.gapWidth = gapWidth; bulletColor = color; isBullet = true; return this; } /** * 设置字体比例 * * @param proportion 比例 * @return {@link Builder} */ public Builder setProportion(float proportion) { this.proportion = proportion; return this; } /** * 设置字体横向比例 * * @param proportion 比例 * @return {@link Builder} */ public Builder setXProportion(float proportion) { this.xProportion = proportion; return this; }

你可能感兴趣的:(转:工具类之SpannableStringUtils(相信你会爱上它))