1

I'm using Xamarin.TTTAttributedLabel to parse HTML in xamarin.ios app. Most of the times, whenever I click on the link it freezes the app. Sometimes it works and sometimes it doesn't. I'm using Label inside UIView --> ScrollView. My scrollview doesn't have any tap gesture. This doesn't happen on simulator. Only on physical device.

View.AddSubview (scrollView)
TTTAttributedLabel lbl = new TTTAttributedLabel();
lbl.SetText(attributedString, 15f);
lbl.Lines = 0;
lbl.EnabledTextCheckingTypes = NSTextCheckingType.Link | NSTextCheckingType.PhoneNumber;
lbl.UserInteractionEnabled = true;
lbl.Delegate = new TTTHTMLLabelDelegate(this);
lbl.Frame = new CGRect (15, 0, Common.screenWidth - 30, 20);
lbl.SizeToFit();
view2.AddSubview(lbl);
scrollView.AddSubview(view2);



//Delegate code
public override void DidSelectLinkWithURL(TTTAttributedLabel label, NSUrl url)
        {
            if (url != null)
            {
                var PageUrl = url.ToString();
                if (!string.IsNullOrWhiteSpace(PageUrl))
                {
                    if (PageUrl.StartsWith("mailto"))
                    {
                        //code to open email app
                    }
                    else if (PageUrl.StartsWith("tel"))
                    {
                       //code to open phone app

                    }
                    else
                    {
                        //for <a> links
                        UIApplication.SharedApplication.OpenUrl(url)
                    }
                }
            }

        }

UPDATED with sample code. I have observed one more thing, DidSelectLinkWithURL not get called when app freezes.

Anubhav
  • 93
  • 1
  • 8

1 Answers1

1

Try to store your variable

public class YourClass
{
TTTAttributedLabel lbl;

 ViewDidLoad()
 {
       View.AddSubview (scrollView)
       lbl = new TTTAttributedLabel();
       lbl.SetText(attributedString, 15f);
       lbl.Lines = 0;
       lbl.EnabledTextCheckingTypes = NSTextCheckingType.Link | 
       NSTextCheckingType.PhoneNumber;
       lbl.UserInteractionEnabled = true;
       lbl.Delegate = new TTTHTMLLabelDelegate(this);
       lbl.Frame = new CGRect (15, 0, Common.screenWidth - 30, 20);
       lbl.SizeToFit();
       view2.AddSubview(lbl);
       scrollView.AddSubview(view2);
 }
}
Nikita
  • 26
  • 2
  • 1
    thanks @nikita. Last week, I solved the issue by declaring label at class level as you suggested. Initializing inside ViewDidLoad() didn't work for me. So I initialized inside constructor, which fixed the issue for me. One more thing, if you have an array of TTTAttributedLabel then you have to initialize **single element**. Like `lbl[i] = new TTTAttributedLabel();` Just writing `lbl = new TTTAttributedLabel[SIZE]` won't work. – Anubhav Aug 09 '18 at 08:10
  • If you initialize array with [size] all elements will get its default value. Thats why you should initialize every element – Nikita Aug 16 '18 at 06:50