I want to create tappable text when I tap on those text then fire some action like call any function do some operation of that text I catnap on any text not on whole uitextview
-
1on complete text or any particular text? – Shezad Sep 05 '18 at 08:06
-
https://stackoverflow.com/questions/45589324/uitextview-with-hyperlink-text-in-swift ? – Ahmad F Sep 05 '18 at 08:12
-
Are you intended to tap on the links within UITextField text ? OR you intend to get this action on complete text ? – Rizwan Sep 05 '18 at 08:22
-
You should try this link : https://stackoverflow.com/a/46410358/2847388 Hope it will work. – SThakur Sep 05 '18 at 10:20
-
any particular text – blkerai Sep 05 '18 at 10:56
4 Answers
Try this -
override func viewDidLoad() {
super.viewDidLoad()
// You must set the formatting of the link manually
let linkAttributes: [NSAttributedStringKey: Any] = [
.link: NSURL(string: "https://www.apple.com")!,
.foregroundColor: UIColor.blue
]
let attributedString = NSMutableAttributedString(string: "Just click here to register")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10))
self.textView.delegate = self
self.textView.attributedText = attributedString
self.textView.isUserInteractionEnabled = true
self.textView.isEditable = false
}

- 383
- 3
- 18
If I understand your question correctly, you want to click on a text view and call a function. You can use UITapGestureRecognizer.
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelTapped))
textView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func labelTapped(){
print("Do something")
}

- 490
- 2
- 13
I've faced with problem same like your and I've tried many things to resolve. The best approach will be use Atributika (or similar) library. You will not regret this decision. It's easy to use and have a lot features.

- 786
- 1
- 6
- 16
If I understand your question properly then you could just add a button then change the button text to the text you want. Then like normal text you can mess with the colour, border or fill. After you have added this to your app link it up as an action with your view controller.swift file
Here is an example from my app 'Health Dash':
Then the swift 4 code:
import UIKit
class FriendsViewController: UIViewController {
@IBAction func exampleText(_ sender: Any) {
print("When you click this button something happens")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Here is an image of what it should look like:

- 142
- 12