public void setText(String text) { int startIndex = 0; while (true) { int start = text.IndexOf("《"); int end = text.IndexOf("》"); if (start < 0 || end < 0) { Append(text.Substring(startIndex)); break; } Append(text.Substring(startIndex, start)); SpannableString spanableInfo = new SpannableString(text.Substring(start, end + 1)); spanableInfo.SetSpan(new Clickable(Context, spanableInfo.ToString()), 0, end + 1 - start, SpanTypes.ExclusiveExclusive); Append(spanableInfo); //setMovementMethod()该方法必须调用,否则点击事件不响应 MovementMethod = LinkMovementMethod.Instance; text = text.Substring(end + 1); LogUtils.e(spanableInfo.ToString()); } }
private static SpannableString AddImages(PostDataObject item, Context context, SpannableString spendable) { try { //Regex pattern that looks for embedded images of the format: [img src=imageName/] //exp. This [img src=imageName/] is an icon. Pattern refImg = Pattern.Compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E"); //bool hasChanges = false; Matcher matcher = refImg.Matcher(spendable); while (matcher.Find()) { bool set = true; foreach (var span in spendable.GetSpans(matcher.Start(), matcher.End(), Class.FromType(typeof(ImageSpan)))) { if (spendable.GetSpanStart(span) >= matcher.Start() && spendable.GetSpanEnd(span) <= matcher.End()) { spendable.RemoveSpan(span); } else { set = false; break; } } if (set) { string resName = spendable.SubSequence(matcher.Start(1), matcher.End(1))?.Trim(); int id = context.Resources.GetIdentifier(resName, "drawable", context.PackageName); var d = ContextCompat.GetDrawable(context, id); if (d != null) { d.SetBounds(0, 0, d.IntrinsicWidth, d.IntrinsicHeight); spendable.SetSpan(new ImageSpan(d, SpanAlign.Baseline), matcher.Start(), matcher.End(), SpanTypes.ExclusiveExclusive); } else { spendable.SetSpan(new ImageSpan(context, id, SpanAlign.Baseline), matcher.Start(), matcher.End(), SpanTypes.ExclusiveExclusive); } //hasChanges = true; } } var username = WoWonderTools.GetNameFinal(Publisher); SetTextStyle(spendable, username, TypefaceStyle.Bold); if (spendable.ToString() !.Contains(context.GetText(Resource.String.Lbl_ChangedProfileCover))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_ChangedProfileCover), "#888888"); } else if (spendable.ToString() !.Contains(context.GetText(Resource.String.Lbl_ChangedProfilePicture))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_ChangedProfilePicture), "#888888"); }
private void UpdateFormattedText() { if (Element?.FormattedText == null) { return; } var extensionType = typeof(FormattedStringExtensions); var type = extensionType.GetNestedType("FontSpan", BindingFlags.NonPublic); var ss = new SpannableString(Control.TextFormatted); var spans = ss.GetSpans(0, ss.ToString().Length, Class.FromType(type)); foreach (var span in spans) { var font = (Font)type.GetProperty("Font").GetValue(span, null); if ((font.FontFamily ?? Element.FontFamily) != null) { var start = ss.GetSpanStart(span); var end = ss.GetSpanEnd(span); var flags = ss.GetSpanFlags(span); ss.RemoveSpan(span); var newSpan = new CustomTypefaceSpan(Control, Element, font); ss.SetSpan(newSpan, start, end, flags); } } Control.TextFormatted = ss; }
private void SetTextWithCustomFont(TextView text, String fontName) { if (Text != null) { var s = new SpannableString(Text); s.SetSpan(new TypefaceSpan(text.Context, fontName), 0, s.Length(), SpanTypes.ExclusiveExclusive); text.Text = s.ToString(); } }
private static void SetTextStyle(SpannableString spendable, string texts, TypefaceStyle style) { try { string content = spendable.ToString(); spendable.SetSpan(new StyleSpan(style), content.IndexOf(texts, StringComparison.Ordinal), content.IndexOf(texts, StringComparison.Ordinal) + texts.Length, SpanTypes.ExclusiveExclusive); } catch (Exception e) { Console.WriteLine(e); } }
private static void SetTextColor(SpannableString spendable, string texts, string color, float proportion = 0.9f) { try { string content = spendable.ToString(); spendable.SetSpan(new ForegroundColorSpan(Color.ParseColor(color)), content.IndexOf(texts, StringComparison.Ordinal), /* content.IndexOf(texts, StringComparison.Ordinal) +*/ content.Length, SpanTypes.ExclusiveExclusive); spendable.SetSpan(new RelativeSizeSpan(proportion), content.IndexOf(texts, StringComparison.Ordinal), /*content.IndexOf(texts, StringComparison.Ordinal) +*/ content.Length, SpanTypes.ExclusiveExclusive); } catch (Exception e) { Console.WriteLine(e); } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); this.SetContentView(Resource.Layout.activity_playground); //Show how to style the text of an existing TextView TextView tv1 = this.FindViewById <TextView>(Resource.Id.test1); new Iconics.IconicsBuilder().Ctx(this) .Style(new ForegroundColorSpan(Color.White), new BackgroundColorSpan(Color.Black), new RelativeSizeSpan(2f)) .StyleFor("faw-adjust", new BackgroundColorSpan(Color.Red), new ForegroundColorSpan(Color.ParseColor("#33000000")), new RelativeSizeSpan(2f)) .On(tv1) .Build(); //You can also do some advanced stuff like setting an image within a text TextView tv2 = this.FindViewById <TextView>(Resource.Id.test5); SpannableString sb = new SpannableString(tv2.Text); IconicsDrawable d = new IconicsDrawable(this, FontAwesome.Icon.FawAndroid).SizeDp(48).PaddingDp(4); sb.SetSpan(new ImageSpan(d, SpanAlign.Bottom), 1, 2, SpanTypes.ExclusiveExclusive); tv2.Text = sb.ToString(); //Set the icon of an ImageView (or something else) as drawable ImageView iv2 = this.FindViewById <ImageView>(Resource.Id.test2); iv2.SetImageDrawable(new IconicsDrawable(this, FontAwesome.Icon.FawThumbsOUp).SizeDp(48).Color(Color.ParseColor("#aaFF0000")).ContourWidthDp(1)); //Set the icon of an ImageView (or something else) as bitmap ImageView iv3 = this.FindViewById <ImageView>(Resource.Id.test3); iv3.SetImageBitmap(new IconicsDrawable(this, new FontAwesome(), FontAwesome.Icon.FawAndroid).Color(Color.ParseColor("#deFF0000")).ToBitmap()); //Show how to style the text of an existing button (NOT WORKING AT THE MOMENT) Button b4 = this.FindViewById <Button>(Resource.Id.test4); new Iconics.IconicsBuilder().Ctx(this) .Style(new BackgroundColorSpan(Color.Black)) .Style(new RelativeSizeSpan(2f)) .Style(new ForegroundColorSpan(Color.White)) .On(b4) .Build(); }
public override void convert(EasyLVHolder holder, int position, BookMark item) { TextView tv = holder.getView <TextView>(Resource.Id.tvMarkItem); SpannableString spanText = new SpannableString((position + 1) + ". " + item.title + ": "); spanText.SetSpan(new ForegroundColorSpan(new Color(ContextCompat.GetColor(mContext, Resource.Color.light_coffee))), 0, spanText.Length(), SpanTypes.InclusiveExclusive); tv.Text = spanText.ToString(); if (item.desc != null) { tv.Append(item.desc .Replace(" ", "") .Replace(" ", "") .Replace("\n", "")); } }
private void CreateLink(ref SpannableString converted, IBaseFont font, FontIndexPair pair) { //if theres a link property, use that one, if not, use the text itself string link; if (pair.TagProperties != null && pair.TagProperties.ContainsKey("href")) { link = pair.TagProperties.GetValueOrDefault("href"); } else { link = converted.ToString().Substring(pair.StartIndex, pair.EndIndex - pair.StartIndex).Trim(); } converted.SetSpan(new ClickableLinkSpan() { Link = link }, pair.StartIndex, pair.EndIndex, SpanTypes.ExclusiveInclusive); _clickableFont = font; _containsLink = true; }
private void SetMarkerSnippet(Marker marker, View view) { string markerSnippet = marker.Snippet; if (marker.Tag != null) { markerSnippet = (string)marker.Tag; } TextView snippetView = ((TextView)view.FindViewById(Resource.Id.txtv_snippett)); if (!String.IsNullOrEmpty(markerSnippet)) { SpannableString snippetText = new SpannableString(markerSnippet); snippetText.SetSpan(new ForegroundColorSpan(Color.Red), 0, markerSnippet.Length, 0); snippetView.Text = snippetText.ToString(); } else { snippetView.Text = (""); } }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { base.OnCreateView(inflater, container, savedInstanceState); var view = inflater.Inflate(Resource.Layout.SavedProperties, container, false); //this.Activity.SetTitle(Resource.String.SavedProperties); string title = GetString(Resource.String.SavedProperties); SpannableString s = new SpannableString(title); s.SetSpan(new ForegroundColorSpan(Color.White), 0, title.Length, SpanTypes.ExclusiveExclusive); this.Activity.Title = s.ToString(); //this.Activity.Title = Html.FromHtml("<font color='ffffff'>" + GetString(Resource.String.SavedProperties) + "</font>").ToString(); int count = GetCount(); List <Property> properties = GetSavedProperties(); if (count == 0 || properties == null || properties.Count == 0) { var textView = view.FindViewById <TextView>(Resource.Id.NoPropSaved); textView.Visibility = ViewStates.Visible; var elv = view.FindViewById <ExpandableListView>(Resource.Id.elvSavedProperties); elv.Visibility = ViewStates.Invisible; } else { var textView = view.FindViewById <TextView>(Resource.Id.NoPropSaved); textView.Visibility = ViewStates.Gone; var elv = view.FindViewById <ExpandableListView>(Resource.Id.elvSavedProperties); elv.Visibility = ViewStates.Visible; var expandListener = new SavedPropertiesExpandableDataAdapter(this.Activity, properties); elv.SetAdapter(expandListener); elv.GroupExpand += Elv_GroupExpand; } RetainInstance = true; return(view); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); this.SetContentView(Resource.Layout.activity_playground); //Show how to style the text of an existing TextView TextView tv1 = this.FindViewById<TextView>(Resource.Id.test1); new Iconics.IconicsBuilder().Ctx(this) .Style(new ForegroundColorSpan(Color.White), new BackgroundColorSpan(Color.Black), new RelativeSizeSpan(2f)) .StyleFor("faw-adjust", new BackgroundColorSpan(Color.Red), new ForegroundColorSpan(Color.ParseColor("#33000000")), new RelativeSizeSpan(2f)) .On(tv1) .Build(); //You can also do some advanced stuff like setting an image within a text TextView tv2 = this.FindViewById<TextView>(Resource.Id.test5); SpannableString sb = new SpannableString(tv2.Text); IconicsDrawable d = new IconicsDrawable(this, FontAwesome.Icon.FawAndroid).SizeDp(48).PaddingDp(4); sb.SetSpan(new ImageSpan(d, SpanAlign.Bottom), 1, 2, SpanTypes.ExclusiveExclusive); tv2.Text = sb.ToString(); //Set the icon of an ImageView (or something else) as drawable ImageView iv2 = this.FindViewById<ImageView>(Resource.Id.test2); iv2.SetImageDrawable(new IconicsDrawable(this, FontAwesome.Icon.FawThumbsOUp).SizeDp(48).Color(Color.ParseColor("#aaFF0000")).ContourWidthDp(1)); //Set the icon of an ImageView (or something else) as bitmap ImageView iv3 = this.FindViewById<ImageView>(Resource.Id.test3); iv3.SetImageBitmap(new IconicsDrawable(this, new FontAwesome(), FontAwesome.Icon.FawAndroid).Color(Color.ParseColor("#deFF0000")).ToBitmap()); //Show how to style the text of an existing button (NOT WORKING AT THE MOMENT) Button b4 = this.FindViewById<Button>(Resource.Id.test4); new Iconics.IconicsBuilder().Ctx(this) .Style(new BackgroundColorSpan(Color.Black)) .Style(new RelativeSizeSpan(2f)) .Style(new ForegroundColorSpan(Color.White)) .On(b4) .Build(); }
private void SetMarkerTextView(Marker marker, View view) { string markerTitle = marker.Title; TextView titleView = null; var obj = view.FindViewById(Resource.Id.txtv_titlee); if (obj is TextView) { titleView = (TextView)obj; } if (markerTitle == null) { titleView.Text = ""; } else { SpannableString titleText = new SpannableString(markerTitle); titleText.SetSpan(new ForegroundColorSpan(Color.Blue), 0, titleText.Length(), 0); titleView.Text = titleText.ToString(); } }
public static void RecalculateSpanPositions(this TextView textView, Label element, SpannableString spannableString, SizeRequest finalSize) { if (element?.FormattedText?.Spans == null || element.FormattedText.Spans.Count == 0) { return; } var labelWidth = finalSize.Request.Width; if (labelWidth <= 0 || finalSize.Request.Height <= 0) { return; } var layout = textView.Layout; if (layout == null) { return; } var text = spannableString.ToString(); int next = 0; int count = 0; IList <int> totalLineHeights = new List <int>(); for (int i = 0; i < spannableString.Length(); i = next) { var type = Java.Lang.Class.FromType(typeof(Java.Lang.Object)); var span = element.FormattedText.Spans[count]; count++; if (string.IsNullOrEmpty(span.Text)) { continue; } // find the next span next = spannableString.NextSpanTransition(i, spannableString.Length(), type); // get all spans in the range - Android can have overlapping spans var spans = spannableString.GetSpans(i, next, type); var startSpan = spans[0]; var endSpan = spans[spans.Length - 1]; var startSpanOffset = spannableString.GetSpanStart(startSpan); var endSpanOffset = spannableString.GetSpanEnd(endSpan); var startX = layout.GetPrimaryHorizontal(startSpanOffset); var endX = layout.GetPrimaryHorizontal(endSpanOffset); var startLine = layout.GetLineForOffset(startSpanOffset); var endLine = layout.GetLineForOffset(endSpanOffset); double[] lineHeights = new double[endLine - startLine + 1]; // calculate all the different line heights for (var lineCount = startLine; lineCount <= endLine; lineCount++) { var lineHeight = layout.GetLineBottom(lineCount) - layout.GetLineTop(lineCount); lineHeights[lineCount - startLine] = lineHeight; if (totalLineHeights.Count <= lineCount) { totalLineHeights.Add(lineHeight); } } var yaxis = 0.0; for (var line = startLine; line > 0; line--) { yaxis += totalLineHeights[line]; } ((ISpatialElement)span).Region = Region.FromLines(lineHeights, labelWidth, startX, endX, yaxis).Inflate(10); } }
private static void AddImages(PostDataObject item, Context context, SpannableString spendable) { try { //Regex pattern that looks for embedded images of the format: [img src=imageName/] //exp. This [img src=imageName/] is an icon. Pattern refImg = Pattern.Compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E"); //bool hasChanges = false; Matcher matcher = refImg.Matcher(spendable); while (matcher.Find()) { bool set = true; foreach (var span in spendable.GetSpans(matcher.Start(), matcher.End(), Class.FromType(typeof(ImageSpan)))) { if (spendable.GetSpanStart(span) >= matcher.Start() && spendable.GetSpanEnd(span) <= matcher.End()) { spendable.RemoveSpan(span); } else { set = false; break; } } if (set) { string resName = spendable.SubSequence(matcher.Start(1), matcher.End(1)).Trim(); int id = context.Resources.GetIdentifier(resName, "drawable", context.PackageName); var d = ContextCompat.GetDrawable(context, id); if (d != null) { d.SetBounds(0, 0, d.IntrinsicWidth, d.IntrinsicHeight); spendable.SetSpan(new ImageSpan(d, SpanAlign.Baseline), matcher.Start(), matcher.End(), SpanTypes.ExclusiveExclusive); } else { spendable.SetSpan(new ImageSpan(context, id, SpanAlign.Baseline), matcher.Start(), matcher.End(), SpanTypes.ExclusiveExclusive); } //hasChanges = true; } } var username = WoWonderTools.GetNameFinal(Publisher); SetTextStyle(spendable, username, TypefaceStyle.Bold); if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_ChangedProfileCover))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_ChangedProfileCover), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_ChangedProfilePicture))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_ChangedProfilePicture), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_WasLive))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_WasLive), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_IsListeningTo))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_IsListeningTo), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_IsPlaying))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_IsPlaying), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_IsTravelingTo))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_IsTravelingTo), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_IsWatching))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_IsWatching), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_AddedNewProductForSell))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_AddedNewProductForSell), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_CreatedNewArticle))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_CreatedNewArticle), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_CreatedNewEvent)) || (item.Event?.EventClass != null && item.SharedInfo.SharedInfoClass == null)) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_CreatedNewEvent), "#888888"); SetTextColor(spendable, Methods.FunString.DecodeString(item.Event?.EventClass.Name), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_addedNewPhotosTo))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_addedNewPhotosTo), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_CreatedNewFund))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_CreatedNewFund), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_OfferPostAdded))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_OfferPostAdded), "#888888"); } else if (spendable.ToString().Contains(context.GetText(Resource.String.Lbl_SharedPost))) { SetTextColor(spendable, context.GetText(Resource.String.Lbl_SharedPost), "#888888"); } //return hasChanges; } catch (Exception e) { Console.WriteLine(e); } }