summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs')
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs b/Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs
new file mode 100644
index 00000000..2a65c98f
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/Renderers/FormattedStringExtensions.cs
@@ -0,0 +1,90 @@
+using System.Text;
+using Android.Graphics;
+using Android.Text;
+using Android.Text.Style;
+using Android.Util;
+using Android.Widget;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ public static class FormattedStringExtensions
+ {
+ public static SpannableString ToAttributed(this FormattedString formattedString, Font defaultFont, Color defaultForegroundColor, TextView view)
+ {
+ if (formattedString == null)
+ return null;
+
+ var builder = new StringBuilder();
+ foreach (Span span in formattedString.Spans)
+ {
+ if (span.Text == null)
+ continue;
+
+ builder.Append(span.Text);
+ }
+
+ var spannable = new SpannableString(builder.ToString());
+
+ var c = 0;
+ foreach (Span span in formattedString.Spans)
+ {
+ if (span.Text == null)
+ continue;
+
+ int start = c;
+ int end = start + span.Text.Length;
+ c = end;
+
+ if (span.ForegroundColor != Color.Default)
+ {
+ spannable.SetSpan(new ForegroundColorSpan(span.ForegroundColor.ToAndroid()), start, end, SpanTypes.InclusiveExclusive);
+ }
+ else if (defaultForegroundColor != Color.Default)
+ {
+ spannable.SetSpan(new ForegroundColorSpan(defaultForegroundColor.ToAndroid()), start, end, SpanTypes.InclusiveExclusive);
+ }
+
+ if (span.BackgroundColor != Color.Default)
+ {
+ spannable.SetSpan(new BackgroundColorSpan(span.BackgroundColor.ToAndroid()), start, end, SpanTypes.InclusiveExclusive);
+ }
+
+ if (!span.IsDefault())
+ spannable.SetSpan(new FontSpan(span.Font, view), start, end, SpanTypes.InclusiveInclusive);
+ else if (defaultFont != Font.Default)
+ spannable.SetSpan(new FontSpan(defaultFont, view), start, end, SpanTypes.InclusiveInclusive);
+ }
+ return spannable;
+ }
+
+ class FontSpan : MetricAffectingSpan
+ {
+ public FontSpan(Font font, TextView view)
+ {
+ Font = font;
+ TextView = view;
+ }
+
+ public Font Font { get; }
+
+ public TextView TextView { get; }
+
+ public override void UpdateDrawState(TextPaint tp)
+ {
+ Apply(tp);
+ }
+
+ public override void UpdateMeasureState(TextPaint p)
+ {
+ Apply(p);
+ }
+
+ void Apply(Paint paint)
+ {
+ paint.SetTypeface(Font.ToTypeface());
+ float value = Font.ToScaledPixel();
+ paint.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, value, TextView.Resources.DisplayMetrics);
+ }
+ }
+ }
+} \ No newline at end of file