0

Hi I have to push a viewController on click of HyperLink. I have NSMutableAttributedString text. that attributed text have two hyperlink examples GoToHomeScreen and aboutUs both are hyperLink on click of aboutUs need to open a link which is working fine

    let attributesString = NSMutableAttributedString(string: "GoToHomeScreen for know more about app and for info aboutUs ")
    attributesString.addAttribute(.link, value: "https://apple.com/aboutUs", range: NSRange(location: 50, length: 7))
    textFields.isEditable = false
    textFields.delegate = self
    textFields.attributedText = attributesString

to open aboutUs working fine but how can we push a view controller on click of GoToHomeScreen

Note:- I am using UITextView to perform it, I can't use button

Sanjay Mishra
  • 672
  • 7
  • 17
  • 2
    Add a custom link like "myApp://goToHomeScreen", and implement UITextViewDelegate method `textView(_:shouldInteractWith:in:interaction:)`, detect the link, if it's a custom one (for aboutUS), push VC and return `false` in that method, else return true (let normal behavior, like opening Safari.app). – Larme Sep 13 '21 at 13:14
  • is GoToHomeScreen text is the link ? – Raja Kishan Sep 13 '21 at 13:14
  • @RajaKishan yes GoToHomeScreen is link as a text – Sanjay Mishra Sep 13 '21 at 13:17

1 Answers1

2

You need to add also a link for GoToHomeScreen.

attributesString.addAttribute(.link, value: "myApp://GoToHomeScreen", range: ...)

Since you have already set the delegate, implements textView(_:shouldInteractWith:in:interaction:):

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

    if URL.absoluteString == "myApp://GoToHomeScreen" {
        pushHomeScreenVC()
        return false
    }
    return true
}
Larme
  • 24,190
  • 6
  • 51
  • 81