匹配<a> anchor tags in string, extract their `href`

时间:2017-05-05 21:21:57

标签: ios swift

I am trying to link all tags in a string to their respective href's so it can be tapped and opened in browser.

This is what I have,

<p>This is a simple text with some embedded <a href="http://example.com/link/to/some/page?param1=77&param2=22">links</a>. 
This is a <a href="https://exmp.le/sample-page/?uu=1">different link</a>.

How do I process this text and link all anchor tags to their respective href?

pseudo code of what I want to achieve:

let finalContentLabel = TTTAttributedLabel(frame: CGRect.zero)
for (linkText, linkHref) in paragraph.anchorTags() {
    let range:NSRange = (finalContentLabel.text as! NSString).range(of: linkText)
    finalContentLabel.addLink(to: URL(string: linkHref), with: range)
}

I have figured out everything but paragraph.anchorTags() bit.

0 个答案:

没有答案