Skip to content

Instantly share code, notes, and snippets.

@puke3615
Created July 14, 2021 07:58
Show Gist options
  • Save puke3615/997a96ab4f6e55bbbb55e5b7453e6ce1 to your computer and use it in GitHub Desktop.
Save puke3615/997a96ab4f6e55bbbb55e5b7453e6ce1 to your computer and use it in GitHub Desktop.
StyleSpan
import android.text.TextPaint;
import android.text.style.CharacterStyle;
/**
* @author puke
* @version 2020-11-10
*/
public class StyleSpan extends CharacterStyle {
private final boolean bold;
private final int size;
private final int color;
private final boolean underline;
private StyleSpan(Builder builder) {
bold = builder.bold;
size = builder.size;
color = builder.color;
underline = builder.underline;
}
public static Builder builder() {
return new Builder();
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setFakeBoldText(bold);
if (size > 0) {
tp.setTextSize(size);
}
if (color > 0) {
tp.setColor(color);
}
tp.setUnderlineText(underline);
}
public static final class Builder {
private boolean bold = false;
private int size = 0;
private int color = 0;
private boolean underline = false;
private Builder() {
}
public Builder bold(boolean bold) {
this.bold = bold;
return this;
}
public Builder size(int size) {
this.size = size;
return this;
}
public Builder color(int color) {
this.color = color;
return this;
}
public Builder underline(boolean underline) {
this.underline = underline;
return this;
}
public StyleSpan build() {
return new StyleSpan(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment