preface

Recently, I have been working on the rich text processing of TextView. I came into contact with some problems such as fine bold, font color, inline image, ellipsize, LinkMovementMethod pit and ClickableSpan click event. After looking up information, look at the source code and do three experiments, the Rich text processing of Android TextView has a small experience, write a blog summary of their own harvest, in the future similar problems are also convenient to review.

android.text.style

Rich text related xxxSpan is mostly in this package, android-28 has 42 files, including a variety of font Settings, custom draw, click events, paragraph style Span.

1, CharacterStyle (Come on, you pick the brush)

The base class of most spans has an abstract method: public Abstract void updateDrawState(TextPaint TP); TextPaint inherits from Paint, which gives us a chance to manipulate the brush, setting the foreground color, background color, font color, font thickness, font size, and so on.

I: Ok, BOLD design: No, this is too thick. I: I...Copy the code

SetFakeBoldText works in this case. Instead of using a bold font, use paint.setstrokeWidth to make the brush thicker. Attached is a link to solve the problem: how to achieve “the middle words should be bold, but not too thick, relatively slim thick”?

2, UpdateAppearance

UpdateLayout
CharacterStyle
updateDrawState

3, ClickableSpan

Added an abstract method public abstract void onClick(@nonnull View widget); In response to a click event (external coordination is required, such as LinkMovementMethod or OnTouchListener). URLSpan inherits from it and stores a URL that is opened with startActivity when clicked.

4, UpdateLayout

UpdateAppearance
MetricAffectingSpan
public abstract void updateMeasureState(@NonNull TextPaint textPaint);

5. ReplacementSpan

Paint
ReplacementSpan
MetricAffectingSpan
public abstract int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm);
public abstract void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint);
ReplacementSpan

6, Other Span

There are also a lot of spans related to ParagraphStyle, which mainly goes with ParagraphStyle, which I’ve never used before, so there you go

TtsSpan
SuggestionSpan
EasyEditSpan
SpellCheckSpan
SuggestionRangeSpan

The last

Android offers so many spans that I use ReplacementSpan and ClickableSpan most of the time. ReplacementSpan is used for real-world custom emojis and inline ICONS. ClickableSpan is used to customize click events, such as clicking @somebody, # topic #, viewing an image, and so on, using a local page. These two spans are used a lot and pit a lot. Next, I will make a detailed summary of these two spans. Html. FromHtml implementation of rich text processing is actually using the Span implementation provided by Android, but TextView supports very limited tags, do some simple processing is ok, after all, TextView mainly displays text, why the whole so complicated. I mostly use hyperlinks and custom ClickableSpan and Route to open local pages.