private string ParseText(string text) { string result = text; if (!string.IsNullOrEmpty(text)) { Regex pattern = new Regex("\\[\\[AS_[A-Za-z-]*\\]\\]"); //Linkify.AddLinks(textView, pattern, "taxon://",null,new TaxonTransformFilter()); foreach (Match m in pattern.Matches(result)) { var syn = TaxonSynonym.GetByPattern(m.Groups[0].Value); if (syn != null) { var taxon = ((App)App.Current).Taxa.FirstOrDefault(i => i.TaxonId == syn.TaxonId && i.HasDiagnosis); if (taxon != null) { //result = result.Replace(syn.Pattern, string.Format("<a href=\"http://www.insekten-sachsen.de/Pages/TaxonomyBrowser.aspx?Id={0}\">{1}</a>", syn.TaxonId, syn.Text)); result = result.Replace(syn.Pattern, $"<a href=\"taxonid://{syn.TaxonId}\">{syn.Text}</a>"); } else { result = result.Replace(syn.Pattern, syn.Text); } } else //if there is no TaxonSynonym at least hide the [[AS_...]] formating { result = result.Replace(m.Groups[0].Value, m.Groups[0].Value.Substring(5, m.Groups[0].Value.Length - 7)); } } //return "<style>body{font-family: '" + this.Control.Font.Name + "'; font-size:" + this.Control.Font.PointSize + "px;}</style>" + result; } return(result); }
protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); //Control?.SetText(labelText, TextView.BufferType.Spannable); var view = (HtmlLabel)Element; if (view == null || string.IsNullOrEmpty(view.Text)) { return; } #pragma warning disable 618 var labelText = (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.N) ? Html.FromHtml(Element.Text, FromHtmlOptions.ModeLegacy) : Html.FromHtml(Element.Text); #pragma warning restore 618 TextView textView = new TextView(this.Context); textView.LayoutParameters = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent); textView.SetTextColor(Xamarin.Forms.Color.Black.ToAndroid()); // Setting the auto link mask to capture all types of link-able data textView.AutoLinkMask = 0; // Make sure to set text after setting the mask //textView.Text = labelText; SpannableStringBuilder sb = new SpannableStringBuilder(labelText); Pattern pattern = Pattern.Compile("\\[\\[AS_[A-Za-z-]*\\]\\]"); //Linkify.AddLinks(textView, pattern, "taxon://",null,new TaxonTransformFilter()); Matcher m = pattern.Matcher(sb); int diff = 0; while (m.Find()) { var syn = TaxonSynonym.GetByPattern(m.Group(0)); if (syn != null) { int start = m.Start() + diff; int end = m.End() + diff; sb.Replace(start, end, syn.Text); var taxon = ((App)App.Current).Taxa.FirstOrDefault(i => i.TaxonId == syn.TaxonId && i.HasDiagnosis); if (taxon != null) { ClickableSpan cs = new URLSpan("taxonid://" + syn.TaxonId); sb.SetSpan(cs, start, start + syn.Text.Length, SpanTypes.InclusiveInclusive); } diff += syn.Text.Length - syn.Pattern.Length; } else { //if there is no TaxonSynonym at least hide the [[AS_...]] formating int start = m.Start() + diff; int end = m.End() + diff; sb.Replace(start, end, m.Group(0).Substring(5, m.Group(0).Length - 7)); diff += -7; } } textView.SetText(sb, TextView.BufferType.Spannable); textView.SetTextSize(ComplexUnitType.Dip, (float)view.FontSize); if (mm == null) { mm = new TaxonMovementMethod(); mm.LinkClicked += Mm_LinkClicked; } textView.MovementMethod = mm; SetNativeControl(textView); }