Весь мой код, за исключением метода делегата, работает нормально (я добавил дополнительный код для контекста), но вы можете игнорировать его, если хотите, и сосредоточиться на ifs/else ifs и методе делегата.
Предполагается, что метод делегата вызывается при нажатии на ссылку (ссылка и атрибут ссылки работают хорошо), но метод делегата по какой-то причине не вызывается. Может кто-нибудь, пожалуйста, скажите мне, почему и что мне нужно сделать, чтобы это исправить.
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
}
listOfWords
он может быть связан с stackoverflow.com/questions/64041434/ Тестируете?listOfWords.forEach{ if URL(string: $0) == nil { print("it won't work for \($0)" }}
- person Larme   schedule 25.09.2020fake://Hello%20world
, гдеk
будетHello world
. - person Larme   schedule 25.09.2020URL(string: "fake://\(k.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)")
, но я бы заранее проверил, действительно лиk.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
не равен нулю с разверткойif let
. - person Larme   schedule 25.09.2020