Share
Embed
The magic of SpannableString by Samuel Dionne
SpannableString is a powerful concept that goes beyond clickable links in a TextView. We’ll look at different standard use cases and other more daring. We’ll check the different already available Span types and look at how we can create our very own.
https://gdgmontreal.com/2019/11/15/november-meetup/
https://youtu.be/U3cBsubemPc
GDG Montreal
November 27, 2019
Transcript
51 slides
-
Outline What is are Span and Spannable strings What can
we do with them How do they work How to create a custom span Questions -
Differents ways to style text Changing all the text at
once Here is my example text <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="22sp" android:textStyle="bold" /> -
Differents ways to style text Changing only part of the
text Here is my example text <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:magic="???" /> -
Here come the Span Spans are a way to apply
style to different part of a strings -
Here come the Span Spans are a way to apply
style to different part of a strings Here is my example text -
Here come the Span Spans are a way to apply
style to different part of a strings Here is my example text SpannableString str = new SpannableString(“Here is my example”); str.setSpan(new UnderlineSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new ForegroundColorSpan(0xffff0000), 8, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new StyleSpan(BOLC), 19, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); -
Here come the Span str.setSpan(new UnderlineSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Span:
what style we want Span Start index End index Flags -
Here come the Span str.setSpan(new UnderlineSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Span:
what style we want Start index: where to start from Span Start index End index Flags -
Here come the Span str.setSpan(new UnderlineSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Span:
what style we want Start index: where to start from End index: where to end Span Start index End index Flags -
Here come the Span str.setSpan(new UnderlineSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Span:
what style we want Start index: where to start from End index: where to end Flags: what should happen when the text change Span Start index End index Flags -
Text change behaviours Here is my text Spannable.SPAN_EXCLUSIVE_EXCLUSIVE Spannable.SPAN_INCLUSIVE_EXCLUSIVE Spannable.SPAN_EXCLUSIVE_INCLUSIVE
Spannable.SPAN_INCLUSIVE_INCLUSIVE Here is my super-text-er Here is my super-text-er Here is my super-text-er Here is my super-text-er Here is my super-text-er -
How to apply span? It depends of what part can
change SpannedString Text is immutable Spans are immutables -
How to apply span? It depends of what part can
change SpannedString Text is immutable Spans are immutables SpannableString Text is immutable Spans are mutables -
How to apply span? It depends of what part can
change SpannedString Text is immutable Spans are immutables SpannableString Text is immutable Spans are mutables SpannableStringBuilder Text is mutable Spans are mutables -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan • BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan
DynamicDrawableSpan EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan S ubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
Provided spans AbsoluteSizeSpan AlignmentSpan.Standard BackgroundColorSpan BulletSpan CharacterStyle ClickableSpan DrawableMarginSpan DynamicDrawableSpan
EasyEditSpan ForegroundColorSpan IconMarginSpan ImageSpan LeadingMarginSpan.Standard MaskFilterSpan MetricAffectingSpan QuoteSpan RelativeSizeSpan ReplacementSpan ScaleXSpan StrikethroughSpan StyleSpan SubscriptSpan SuggestionSpan SuperscriptSpan TabStopSpan.Standard TextAppearanceSpan -
3 types of Spans Character Affecting Spans Appearance Affecting Spans
Only affect the style No effect on the layout Example: ForegroundColorSpan -
3 types of Spans Character Affecting Spans Appearance Affecting Spans
Only affect the style No effect on the layout Metric Affecting Spans Affect the dimensions of the text Example: AbsoluteSizeSpan -
3 types of Spans Character Affecting Spans Appearance Affecting Spans
Only affect the style No effect on the layout Metric Affecting Spans Affect the dimensions of the text Paragraph Affecting Spans A paragraph is delimited by \n Example: QuoteSpan -
1 Mise en scène 02 2 min 10 min 1
h 25 min -
SuperscriptImageSpan.java public int getSize(Paint paint, CharSequence text, int start, int
end, Paint.FontMetricsInt fm); public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint); -
SuperscriptImageSpan.java public int getSize(Paint paint, CharSequence text, int start, int
end, Paint.FontMetricsInt fm); public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint); -
SuperscriptImageSpan.java public int getSize(Paint paint, CharSequence text, int start, int
end, Paint.FontMetricsInt fm); public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint); -
SuperscriptImageSpan.java public int getSize(Paint paint, CharSequence text, int start, int
end, Paint.FontMetricsInt fm) { Rect rect = drawable.getBounds(); if (fm != null) { fm.top = fm.ascent - rect.bottom / 2; } return rect.right; } -
SuperscriptImageSpan.java public void draw(Canvas canvas, CharSequence text, int start, int
end, float x, int top, int y, int bottom, Paint paint) { canvas.save(); canvas.translate(x, drawable.getBounds().bottom / 2); drawable.draw(canvas); canvas.restore(); } -
References Spantastic text styling with Spans String Manipulation Using SpannableString
Android Developer Doc