First, method introduction
For situations where Kotlin isn’t available or you don’t want to use it, the following wrapper class is easy to use. This tool class is I found on the Internet, after a simple modification, add some functions.
Setbuilder: setFlag: setExtSize: setfont size: setForegroundColor: setBackgroundColor: getBuilder: setBuilder: setFlag: setExtSize: setfont size: setForegroundColor: setBackgroundColor: SetQuoteColor: sets the color of the reference line setLeadingMargin: sets the indentation setBullet: sets the list tag setProportion: sets the font proportion setXProportion: SetStrikethrough: setUnderline: setUnderline: setSuperscript: setSubscript: setBold: setStrikethrough: setUnderline: setUnderline: setSuperscript: setSubscript: setBold: setStrikethrough: setUnderline: setUnderline Settitalic: setItalic setBoldItalic: setFontFamily: Settalign: SetAlignment: setBitmap: setDrawable: SetResourceId: setClickSpan: set click events setUrl: set hyperlink setBlur: setBlur: set append: append style string build: Create style stringCopy the code
2. Introduction to use
2.1 the initialization
There are two ways, either way, to get the spannableStringBuilder object by calling the Build method.
SpannableStringBuilder ssb = new SsbUtils.Builder(this, "src").setItalic().build();
Copy the code
SpannableStringBuilder ssb1 = SsbUtils.getBuilder(this, src1).setBold().build();
Copy the code
2.2 For specific use, the renderings will not be put
-
Sets the entire control text bold
String src1 = "Bold the entire TextView "; SpannableStringBuilder ssb1 = SsbUtils.getBuilder(this, src1).setBold().build(); tvOne.setText(ssb1);Copy the code
-
Set some text bold
String src2 = "set "; SpannableStringBuilder ssB2 = ssButils.getBuilder (this, src2).appEnd (" part ").setbold ().appEnd (" text bold ").build(); SpannableStringBuilder ssb2 = ssButils.getBuilder (this, src2).appEnd (" part "). tvOne2.setText(ssb2);Copy the code
-
Set some text bold + delete line
SpannableStringBuilder ssb3 = SsbUtils.getBuilder(this, "Settings"). Append (" part "). SetBold () setStrikethrough () append (" text bold + delete line "). The build (); tvOne3.setText(ssb3);Copy the code
-
Set the effect of different parts of the text
SpannableStringBuilder ssb4 = SsbUtils.getBuilder(this, SetBoldItalic ().appEnd (" add delete line color ").setStrikeThrough ().setForegroundColor(r.color.teal_700) .build(); tvOne4.setText(ssb4);Copy the code
Third, tools
public class SsbUtils {
private SsbUtils() {
}
/**
* 获取建造者
*
* @return {@link Builder}
*/
public static Builder getBuilder(Context context, @NotNull CharSequence text) {
return new Builder(context, text);
}
public static class Builder {
private int defaultValue = 0x12000000;
private CharSequence text;
private Context context;
private int flag;
@ColorRes
private int foregroundColor;
@ColorRes
private int backgroundColor;
@ColorRes
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;
@DimenRes
private int textSize;
private SpannableStringBuilder mBuilder;
public Builder(Context context, @NotNull CharSequence text) {
this.text = text;
this.context = context;
flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
foregroundColor = defaultValue;
backgroundColor = defaultValue;
quoteColor = defaultValue;
textSize = defaultValue;
proportion = -1;
xProportion = -1;
mBuilder = new SpannableStringBuilder();
}
/**
* 设置标识
*
* @param flag {@link Spanned#SPAN_INCLUSIVE_EXCLUSIVE}
* <p>
* <p>
* {@link Spanned#SPAN_INCLUSIVE_INCLUSIVE}
* <p>
* <p>
* {@link Spanned#SPAN_EXCLUSIVE_EXCLUSIVE}
* <p>
* <p>
* {@link Spanned#SPAN_EXCLUSIVE_INCLUSIVE}
* @return {@link Builder}
*/
public Builder setFlag(int flag) {
this.flag = flag;
return this;
}
/**设置字体大小
* @param textSize
* @return
*/
public Builder setTextSize(@DimenRes int textSize) {
this.textSize = textSize;
return this;
}
/**
* 设置前景色
*
* @param color 前景色
* @return {@link Builder}
*/
public Builder setForegroundColor(@ColorRes int color) {
this.foregroundColor = color;
return this;
}
/**
* 设置背景色
*
* @param color 背景色
* @return {@link Builder}
*/
public Builder setBackgroundColor(@ColorRes int color) {
this.backgroundColor = color;
return this;
}
/**
* 设置引用线的颜色
*
* @param color 引用线的颜色
* @return {@link Builder}
*/
public Builder setQuoteColor(@ColorRes 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;
}
/**
* 设置删除线
*
* @return {@link Builder}
*/
public Builder setStrikethrough() {
this.isStrikethrough = true;
return this;
}
/**
* 设置下划线
*
* @return {@link Builder}
*/
public Builder setUnderline() {
this.isUnderline = true;
return this;
}
/**
* 设置上标
*
* @return {@link Builder}
*/
public Builder setSuperscript() {
this.isSuperscript = true;
return this;
}
/**
* 设置下标
*
* @return {@link Builder}
*/
public Builder setSubscript() {
this.isSubscript = true;
return this;
}
/**
* 设置粗体
*
* @return {@link Builder}
*/
public Builder setBold() {
isBold = true;
return this;
}
/**
* 设置斜体
*
* @return {@link Builder}
*/
public Builder setItalic() {
isItalic = true;
return this;
}
/**
* 设置粗斜体
*
* @return {@link Builder}
*/
public Builder setBoldItalic() {
isBoldItalic = true;
return this;
}
/**
* 设置字体
*
* @param fontFamily 字体
* <p>
* <p>
* <p>
* monospace
* <p>
* <p>
* serif
* <p>
* <p>
* sans-serif
* @return {@link Builder}
*/
public Builder setFontFamily(@NotNull String fontFamily) {
this.fontFamily = fontFamily;
return this;
}
/**
* 设置对齐
* <p>
* <p>
* <p>
* {@link Alignment#ALIGN_NORMAL}正常
* <p>
* <p>
* {@link Alignment#ALIGN_OPPOSITE}相反
* <p>
* <p>
* {@link Alignment#ALIGN_CENTER}居中
*
* @return {@link Builder}
*/
public Builder setAlign(@NotNull Alignment align) {
this.align = align;
return this;
}
/**
* 设置图片
*
* @param bitmap 图片位图
* @return {@link Builder}
*/
public Builder setBitmap(@NotNull Bitmap bitmap) {
this.bitmap = bitmap;
imageIsBitmap = true;
return this;
}
/**
* 设置图片
*
* @param drawable 图片资源
* @return {@link Builder}
*/
public Builder setDrawable(@NotNull Drawable drawable) {
this.drawable = drawable;
imageIsDrawable = true;
return this;
}
/**
* 设置图片
*
* @param uri 图片uri
* @return {@link Builder}
*/
public Builder setUri(@NotNull Uri uri) {
this.uri = uri;
imageIsUri = true;
return this;
}
/**
* 设置图片
*
* @param resourceId 图片资源id
* @return {@link Builder}
*/
public Builder setResourceId(@DrawableRes int resourceId) {
this.resourceId = resourceId;
imageIsResourceId = true;
return this;
}
/**
* 设置点击事件
* <p>
* 需添加view.setMovementMethod(LinkMovementMethod.getInstance())
*
* @param clickSpan 点击事件
* @return {@link Builder}
*/
public Builder setClickSpan(@NotNull ClickableSpan clickSpan) {
this.clickSpan = clickSpan;
return this;
}
/**
* 设置超链接
* <p>
* 需添加view.setMovementMethod(LinkMovementMethod.getInstance())
*
* @param url 超链接
* @return {@link Builder}
*/
public Builder setUrl(@NotNull String url) {
this.url = url;
return this;
}
/**
* 设置模糊
* <p>
* 尚存bug,其他地方存在相同的字体的话,相同字体出现在之前的话那么就不会模糊,出现在之后的话那会一起模糊
* <p>
* <p>
* <p>
* 推荐还是把所有字体都模糊这样使用
*
* @param radius 模糊半径(需大于0)
* @param style 模糊样式
* <p>
* <p>
* {@link Blur#NORMAL}
* <p>
* <p>
* {@link Blur#SOLID}
* <p>
* <p>
* {@link Blur#OUTER}
* <p>
* <p>
* {@link Blur#INNER}
* @return {@link Builder}
*/
public Builder setBlur(float radius, Blur style) {
this.radius = radius;
this.style = style;
this.isBlur = true;
return this;
}
/**
* 追加样式字符串
*
* @param text 样式字符串文本
* @return {@link Builder}
*/
public Builder append(@NotNull CharSequence text) {
setSpan();
this.text = text;
return this;
}
/**
* 创建样式字符串
*
* @return 样式字符串
*/
public SpannableStringBuilder build() {
setSpan();
return mBuilder;
}
/**
* 设置样式
*/
private void setSpan() {
int start = mBuilder.length();
mBuilder.append(this.text);
int end = mBuilder.length();
if (textSize != defaultValue && context != null) {
int ts = context.getResources().getDimensionPixelOffset(textSize);
mBuilder.setSpan(new AbsoluteSizeSpan(ts), start, end, flag);
textSize = defaultValue;
}
if (foregroundColor != defaultValue && context != null) {
mBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, foregroundColor)), start, end, flag);
foregroundColor = defaultValue;
}
if (backgroundColor != defaultValue && context != null) {
mBuilder.setSpan(new BackgroundColorSpan(ContextCompat.getColor(context, backgroundColor)), start, end, flag);
backgroundColor = defaultValue;
}
if (isLeadingMargin) {
mBuilder.setSpan(new LeadingMarginSpan.Standard(first, rest), start, end, flag);
isLeadingMargin = false;
}
if (quoteColor != defaultValue && context != null) {
mBuilder.setSpan(new QuoteSpan(ContextCompat.getColor(context, quoteColor)), start, end, 0);
quoteColor = defaultValue;
}
if (isBullet) {
mBuilder.setSpan(new BulletSpan(gapWidth, bulletColor), start, end, 0);
isBullet = false;
}
if (proportion != -1) {
mBuilder.setSpan(new RelativeSizeSpan(proportion), start, end, flag);
proportion = -1;
}
if (xProportion != -1) {
mBuilder.setSpan(new ScaleXSpan(xProportion), start, end, flag);
xProportion = -1;
}
if (isStrikethrough) {
mBuilder.setSpan(new StrikethroughSpan(), start, end, flag);
isStrikethrough = false;
}
if (isUnderline) {
mBuilder.setSpan(new UnderlineSpan(), start, end, flag);
isUnderline = false;
}
if (isSuperscript) {
mBuilder.setSpan(new SuperscriptSpan(), start, end, flag);
isSuperscript = false;
}
if (isSubscript) {
mBuilder.setSpan(new SubscriptSpan(), start, end, flag);
isSubscript = false;
}
if (isBold) {
mBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, flag);
isBold = false;
}
if (isItalic) {
mBuilder.setSpan(new StyleSpan(Typeface.ITALIC), start, end, flag);
isItalic = false;
}
if (isBoldItalic) {
mBuilder.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, flag);
isBoldItalic = false;
}
if (fontFamily != null) {
mBuilder.setSpan(new TypefaceSpan(fontFamily), start, end, flag);
fontFamily = null;
}
if (align != null) {
mBuilder.setSpan(new AlignmentSpan.Standard(align), start, end, flag);
align = null;
}
if (imageIsBitmap || imageIsDrawable || imageIsUri || imageIsResourceId) {
if (context != null) {
if (imageIsBitmap) {
mBuilder.setSpan(new ImageSpan(context, bitmap), start, end, flag);
bitmap = null;
imageIsBitmap = false;
} else if (imageIsDrawable) {
mBuilder.setSpan(new ImageSpan(drawable), start, end, flag);
drawable = null;
imageIsDrawable = false;
} else if (imageIsUri) {
mBuilder.setSpan(new ImageSpan(context, uri), start, end, flag);
uri = null;
imageIsUri = false;
} else {
mBuilder.setSpan(new ImageSpan(context, resourceId), start, end, flag);
resourceId = 0;
imageIsResourceId = false;
}
}
}
if (clickSpan != null) {
mBuilder.setSpan(clickSpan, start, end, flag);
clickSpan = null;
}
if (url != null) {
mBuilder.setSpan(new URLSpan(url), start, end, flag);
url = null;
}
if (isBlur) {
mBuilder.setSpan(new MaskFilterSpan(new BlurMaskFilter(radius, style)), start, end, flag);
isBlur = false;
}
flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
}
}
}
Copy the code
This is for Java development purposes, and for Kotlin’s development package SpannableStringBuilder, you can go to this address.