0

All of my code except for the delegate method is working fine (I added the extra code for context) but you can ignore that if you want and focus on the ifs/else ifs and the delegate method.

The delegate method is supposed to be called when a link is clicked (the link and link attribute are working well) but the delegate method is not being called for some reason. Can someone please tell me why and what I need to do to fix it.

    let words = formulaTextView.text
    let split = words?.split(separator: " ")
    let last2words = String((split?.suffix(2).joined(separator: " "))!)
    let first2words = String((split?.prefix(2).joined(separator: " "))!)
    let firstWord = String((split?.prefix(1).joined(separator: " "))!)
    let lastWord = String((split?.suffix(1).joined(separator: " "))!)
    let first3Words = String((split?.prefix(3).joined(separator: " "))!)
    let last3Words = String((split?.suffix(3).joined(separator: " "))!)
    let listOfWords =              [first2words,last2words,lastWord,firstWord,last3Words,first3Words]
    formulaTextView.text = "Formula: \(words!)"
    var y: NSMutableAttributedString?
    for k in listOfWords {
if termStrings.contains(k) {
    if words!.contains(k) && y == nil {             
        let textViewAttributedString = NSMutableAttributedString(string: formulaTextView.text!)
        textViewAttributedString.addAttribute(.font, value: UIFont(name: "Arial", size: 14.0)!, range: NSMakeRange(0, textViewAttributedString.length))
        let attributedString = NSMutableAttributedString(string: k)
        
        attributedString.addAttribute(.font, value: UIFont(name: "Arial", size: 14.0)!, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.underlineStyle, value: 1, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.underlineColor, value: UIColor.link, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.foregroundColor, value: UIColor.link, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.link, value: k, range: NSMakeRange(0, attributedString.length))
        
        formulaTextView.linkTextAttributes = [.foregroundColor: UIColor.link, .font: UIFont(name: "Arial", size: 14.0)!]
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .left

        let range = textViewAttributedString.string.range(of: k)!
        let nsrange = NSRange(range, in: textViewAttributedString.string)
        
        textViewAttributedString.replaceCharacters(in: nsrange, with: attributedString)
        
        y = textViewAttributedString
        
        textViewAttributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, textViewAttributedString.length))
        
        formulaTextView.attributedText = textViewAttributedString
        
    }
    else if words!.contains(k) && y != nil {
        
        let attributedString = NSMutableAttributedString(string: k)
        
        attributedString.addAttribute(.font, value: UIFont(name: "Arial", size: 14.0)!, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.underlineStyle, value: 1, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.underlineColor, value: UIColor.link, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.foregroundColor, value: UIColor.link, range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(.link, value: k, range: NSMakeRange(0, attributedString.length))
        
        formulaTextView.linkTextAttributes = [.foregroundColor: UIColor.link, .font: UIFont(name: "Arial", size: 14.0)!]
        
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .left
        
        let range = y!.string.range(of: k)!
        let nsrange = NSRange(range, in: y!.string)
        
        y!.replaceCharacters(in: nsrange, with: attributedString)
        
        y!.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, y!.length))
        
        
        formulaTextView.attributedText = y!
    }
   }



    // Do any additional setup after loading the view.
}

formulaTextView.isEditable = false
formulaTextView.isSelectable = true
formulaTextView.isUserInteractionEnabled = true
formulaTextView.delegate = self

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    print("hello world")
    return false
}
Neel1005
  • 11
  • 2
  • Depending on `listOfWords`, it could be linked to https://stackoverflow.com/questions/64041434/uitextview-delegate-method-not-calling-in-uitableviewcell-using-swift Testing it? `listOfWords.forEach{ if URL(string: $0) == nil { print("it won't work for \($0)" }}` – Larme Sep 25 '20 at 08:59
  • The url is not supposed to be valid. I want it to run the delegate func when the text is clicked. Is there any way to do that? – Neel1005 Sep 25 '20 at 16:32
  • Create a "fake url" to make it work. Anyways, you'll return false in the delegate method, so you shouldn't care. Like `fake://Hello%20world` where `k` would be `Hello world` – Larme Sep 25 '20 at 16:33
  • I tried with a valid url and even though i am returning false in the delegate method, i am still getting this error 'Failed to open URL www.apple.com: Error Domain=NSOSStatusErrorDomain Code=-50 "invalid input parameters" UserInfo={NSDebugDescription=invalid input parameters, _LSLine=252, _LSFunction=-[_LSDOpenClient openURL:options:completionHandler:]}' – Neel1005 Sep 25 '20 at 16:42
  • Can someone tell me why this is happening? – Neel1005 Sep 25 '20 at 16:56
  • That's an error on iOS13, on iOS12.4 (simulators at least, I didn't check on real device) it wasn't giving that error. I think you can ignore it. – Larme Sep 25 '20 at 16:59
  • @Larme the fake url didnt work it gave a “found nil when unwrapping optional value” – Neel1005 Sep 25 '20 at 16:59
  • Well, I got it work on my end, did you percent escaped the value if needed? Which line caused that error? what word? – Larme Sep 25 '20 at 17:00
  • So the delegate method is working – Neel1005 Sep 25 '20 at 17:00
  • @Larme the line was the line that added the link attributes to the NSMutableAttributedString, also I added logic to percent escape the value if there was a space – Neel1005 Sep 25 '20 at 17:06
  • I can't guess what's wrong, but I'd go with `URL(string: "fake://\(k.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)")`, but I'd check before hand if `k.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)` is really not nil with a `if let` unwrap. – Larme Sep 25 '20 at 17:17

0 Answers0