Nurdin
Nurdin

Reputation: 23893

Convert hashtag into link in UITextView

Is it possible to convert hashtag into link in UITextView? I already did some research but did not find a solution. It's like twitter and facebook. I don't know how to start developing it.

enter image description here

Upvotes: 1

Views: 668

Answers (1)

Shamas S
Shamas S

Reputation: 7549

For this you need to use NSAttributedString. To add a link to a UITextView, you would need something like

    var string:NSMutableAttributedString = NSMutableAttributedString(string: "#some one here")

    string.addAttribute(NSLinkAttributeName, value: "http://www.google.com", range:NSMakeRange(0, 5))
    textView.attributedText = string
    textView.dataDetectorTypes = UIDataDetectorTypes.Link

Also, make sure to set your UITextView as not editable. This way it will directly open the link when clicked.

If you want, you can implement delegate method, where you can decide to open the link or not. Set your class as your delegate for UITextView and implement the following function

- (BOOL)textView:(UITextView *)textView
shouldInteractWithURL:(NSURL *)URL
         inRange:(NSRange)characterRange

In this function, you can go to your other UIViewController.

More help at Apple Doc

Upvotes: 2

Related Questions