0

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

blkerai
  • 341
  • 1
  • 17

4 Answers4

1

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
}
teja_D
  • 383
  • 3
  • 18
0

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")
    }
Azat
  • 490
  • 2
  • 13
0

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.

https://github.com/psharanda/Atributika

MarcinR
  • 786
  • 1
  • 6
  • 16
-1

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':

Image 1

Image 2

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:

Image 3

Toby Clark
  • 142
  • 12