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
}