Sturla
Sturla

Reputation: 3626

How to enable hyperlink in UILabel

What I have is a UILabel (loaded with html) that I put on a UIScrollView.

In the html I have lots of links that I can´t get to "fire-up" when clicked.

I have added UserInteractionEnabled = true both on the UILabel and on the UIScrollview but I can´t get any reaction from the taps on the hyperlinks.

This is the code that create the label with the content.

        var attr = new NSAttributedStringDocumentAttributes();
        var nsError = new NSError();
        attr.DocumentType = NSDocumentType.HTML;

        var entry = "some text and url <a href='http://www.google.com'>url</a>";
        var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);

        contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
        contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width  , scrollView.Frame.Size.Height);
        contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
        contentLabel.Lines = 0;
        contentLabel.SizeToFit();

Maybe this is just wrong.. putting html content into a label? But I don´t want to use a webview since it does not seem to scale that well.

Upvotes: 2

Views: 2188

Answers (2)

ejanowski
ejanowski

Reputation: 538

Why not using a UITextView instead ? Enable Links detection and disable editable and Selectable option.

Upvotes: 2

DarthMike
DarthMike

Reputation: 3481

Adding HTML to UILabel will not make it interactive. Use UITextView or https://github.com/TTTAttributedLabel/TTTAttributedLabel

Upvotes: 2

Related Questions