private void SetStyle() { var styledLabel = (ExtendedLabel)this.Element; if (styledLabel.Children != null && styledLabel.Children.Count > 0) { SpannableStringBuilder builder = new SpannableStringBuilder(); styledLabel.Children.ToList().ForEach(child => { var spannableText = new SpannableString(child.Text); spannableText.SetSpan(new ForegroundColorSpan(child.CustomFont.Color.ToAndroid()), 0, child.Text.Length, SpanTypes.Composing); spannableText.SetSpan(new CustomTypefaceSpan(child.CustomFont), 0, child.Text.Length, SpanTypes.Composing); if (child.Command != null) { var clickableSpan = new CustomClickableSpan(); clickableSpan.Click += (Android.Views.View obj) => { child.Command?.Execute(child.CommandParameter); }; spannableText.SetSpan(clickableSpan, 0, child.Text.Length, SpanTypes.Composing); } builder.Append(spannableText); }); Control.TextFormatted = builder; Control.MovementMethod = new LinkMovementMethod(); return; } if (styledLabel.Text != null && styledLabel.CustomFont != null) { styledLabel.CustomFont.ApplyTo(Control); } }
private static void FormatImages(Context context, ISpannable spannable) { var text = spannable?.ToString(); if (string.IsNullOrWhiteSpace(text)) { return; } var matches = ImageRegex.Matches(text); foreach (var match in matches.ToList()) { var drawable = ContextCompat.GetDrawable(context, PlaceholderImage); drawable.SetBounds(0, 0, drawable.IntrinsicWidth, drawable.IntrinsicHeight); var imageSpan = new ImageSpan(drawable, SpanAlign.Baseline); var clickSpan = new CustomClickableSpan(); clickSpan.Click += (sender, e) => { var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(match.Groups[2].Value)); context.StartActivity(intent); }; spannable.SetSpan(clickSpan, match.Index, match.Index + match.Length, SpanTypes.InclusiveExclusive); spannable.SetSpan(imageSpan, match.Index, match.Index + match.Length, SpanTypes.InclusiveExclusive); if (!int.TryParse(match.Groups[1].Value, out var width)) { width = 250; } width = Math.Min(width, MaxImageWidth); Picasso.With(context).Load(match.Groups[2].Value) .Into(new DrawableTarget(spannable, match.Index, match.Index + match.Length, (int)(width * context.Resources.DisplayMetrics.Density))); } }
private static void FormatYoutube(Context context, ISpannable spannable) { var text = spannable?.ToString(); if (string.IsNullOrWhiteSpace(text)) { return; } var matches = YoutubeRegex.Matches(text); var playIcon = ContextCompat.GetDrawable(context, Resource.Drawable.svg_play); playIcon.SetColorFilter(Color.White, PorterDuff.Mode.SrcIn); playIcon.SetAlpha(150); foreach (var match in matches.ToList()) { var drawable = ContextCompat.GetDrawable(context, PlaceholderImage); drawable.SetBounds(0, 0, drawable.IntrinsicWidth, drawable.IntrinsicHeight); var imageSpan = new ImageSpan(drawable, SpanAlign.Baseline); var clickSpan = new CustomClickableSpan(); clickSpan.Click += (sender, e) => { var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(string.Format(YoutubeLinkUrl, match.Groups[1].Value))); context.StartActivity(intent); }; spannable.SetSpan(clickSpan, match.Index, match.Index + match.Length, SpanTypes.InclusiveExclusive); spannable.SetSpan(imageSpan, match.Index, match.Index + match.Length, SpanTypes.InclusiveExclusive); Picasso.With(context).Load(string.Format(YoutubeThumbnailUrl, match.Groups[1].Value)) .Into(new DrawableTarget(spannable, match.Index, match.Index + match.Length, (int)(250 * context.Resources.DisplayMetrics.Density), playIcon)); } }