abha
abha

Reputation: 73

Web links in textview

I'm working on a medical iOS app. The app has hundreds of references (such as the one below) and at the end of every reference there are some numbers that identify the reference on "www.Pubmed.com". So if the user copy the numbers then use them in the search bar on "www.Pubmed.com", he can find the full article.

Is there any way I can make the numbers as tappable links when pressed by the user can segue directly to the article on the website?

**** Reference example:**

Hendel RC, Berman DS, Di Carli MF, Heidenreich PA, Henkin RE, Pellikka PA, Pohost GM, Williams KA. J Am Coll Cardiol 2009;53:2201–29.

Thanks

UPDATE: I found the answer to my question.

let's say that I want to add a "Click here" text at the end of my staring so I can click it and it will take me to the website. The code to do so will look like this:

let text1 = "Hendel RC, Berman DS, Di Carli MF, Heidenreich PA, Henkin RE, Pellikka PA, Pohost GM, Williams KA. J Am Coll Cardiol 2009;53:2201–29."

let text2 = "Click Here"

let text = text1 + " " + text2 

let attributedString = NSMutableAttributedString(string: text)

let range = (text as NSString).rangeOfString(text2)

attributedString.addAttribute(NSLinkAttributeName, value: "http://www.ncbi.nlm.nih.gov/pubmed/?term=\(text1)", range: range) // fyi you need to convert the text1 to a HTML compatible string first.

self.textView.attributedText = attributedString

This works just fine for me!

Upvotes: 0

Views: 47

Answers (1)

John Leonardo
John Leonardo

Reputation: 598

In Xcode, this feature comes out of the box :) use UITextView object, and however you want to populate that UITextView, that's your choice, but you can go to the sidebar on the right, and in the editor thing, under the alignment and font options, you'll see "detection". There will be a box that says "links", select it :)

Upvotes: 1

Related Questions