如何让警报有正确的词?

How to make the alert have the correct word?

我在 TextView 中有文本,文本中有一些词,在用户单击它的情况下,应出现带有词的警告,例如,这可能意味着将词翻译成某种语言。

我试过用字典,key是文本中的词,alert中的词是值。但它不起作用。有错误字词提醒。

import UIKit

class ViewController: UIViewController , UITextViewDelegate    {
    private let kURLString = "https://www.mywebsite.com"

    let dictionary = ["website" : "Johny" , "visit" : "Bilbo"]

    var keyOne : String?
    var valueOne : String?

    @IBOutlet weak var text: UITextView! {
        didSet{
            text.delegate = self
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let originalText = "Please visit the website for more information."
        let attributedOriginalText = NSMutableAttributedString(string: originalText)

        for (key , value) in dictionary {
            keyOne = key
            valueOne = value
            let linkRange = attributedOriginalText.mutableString.range(of: keyOne!)
            attributedOriginalText.addAttribute(.link, value: kURLString, range: linkRange)

        }

        text.attributedText = attributedOriginalText    
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        if (URL.absoluteString == kURLString) {
            alert(value: valueOne!)
        }
        return false
    }

    func alert (value : String) {
         let alert = UIAlertController (title: nil, message: value, preferredStyle: .alert)

         let restartAction = UIAlertAction(title: "Ок", style: .default , handler : { (UIAlertAction) in
            self.viewDidLoad()
         })

         alert.addAction(restartAction)

         present(alert, animated: true, completion: nil)
    }
}

仅当文本视图可选择但不可编辑时,文本视图中的链接才具有交互性。

设置可编辑为假和可选为真:

text.isEditable = false
text.isSelectable = true

要从 textview 中获取选定的文本,这在您的案例中很关键,请使用以下函数:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
          if let key = textView.text.substring(with: characterRange){
            if let value = dictionary[String(key)]{
                print("text :",value)
                alert(value: value)
            }
        }
        return false
    }

使用字符串扩展获取范围为的子字符串:

extension String {
    func substring(with nsrange: NSRange) -> Substring? {
        guard let range = Range(nsrange, in: self) else { return nil }
        return self[range]
    }
}

您可以通过创建一个下次可重复使用的扩展来完成此操作,如下代码所示:

extension UITextView{
    func textRangeFromNSRange(range:NSRange)->String{
        let myNSString = self.text as NSString
        return  myNSString.substring(with: range)
    }
}

用法:

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

        if (URL.absoluteString == kURLString) {
            alert(value: textView.textRangeFromNSRange(range: characterRange))
        }
        return false
    }

如果你想从你的字典中获取值,你可以这样做:

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

        if (URL.absoluteString == kURLString) {
            alert(value: dictionary[textView.textRangeFromNSRange(range: characterRange)]!)
        }
        return false
    }

注意:确保 textView 中的链接可选择但不可编辑。