Swift 快速前进

Swift 快速前进,swift,Swift,如何创建可链接到特定URL并仅在可单击字符串下划线的属性文本。 如何使用UIC属性化文本,以便点击它将当前视图控制器链接到特定URL class ViewController: UIViewController, UITextViewDelegate { @IBOutlet var textView: UITextView! override func viewDidLoad() { let attributedString = NSMutableAtt

如何创建可链接到特定URL并仅在可单击字符串下划线的属性文本。 如何使用UIC属性化文本,以便点击它将当前视图控制器链接到特定URL

    class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        let attributedString = NSMutableAttributedString(string: "Want to learn iOS? You should visit the best source of free iOS tutorials!")
        attributedString.addAttribute(.link, value: "https://www.hackingwithswift.com", range: NSRange(location: 19, length: 55))

        textView.attributedText = attributedString
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        UIApplication.shared.open(URL, options: [:])
        return false
    }
}
你可以在这里找到所有的详细信息

让超链接在UITextView中可单击相当容易。做 因此,转到视图的属性检查器并选中 “检测”字段中的“链接”选项