UITextView 中的链接在 Swift 4 中不起作用 [英] Link in UITextView not working in Swift 4

查看:43
本文介绍了UITextView 中的链接在 Swift 4 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 iOS 11 和 Swift 4.

I'm using iOS 11 and Swift 4.

我正在尝试以编程方式在 UITextView 中生成一个链接.

I am trying to programmatically generate a link inside a UITextView.

所有属性都有效(即颜色、大小、范围等) - 但不幸的是,链接不起作用.谁能告诉我为什么?

All the attributes work (i.e. color, size, range etc.) - but unfortunately, the link does not work. Can anybody tell me why?

这是我的代码:

@IBOutlet weak var textView: UITextView!

// Setting the attributes
let linkAttributes = [
    NSAttributedStringKey.link: URL(string: "https://www.apple.com")!,
    NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 18.0)!,
    NSAttributedStringKey.foregroundColor: UIColor.blue
    ] as [NSAttributedStringKey : Any]

let attributedString = NSMutableAttributedString(string: "Just click here to do stuff...")

// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10))

self.textView.delegate = self
self.textView.attributedText = attributedString

同样,链接似乎是正确的,但点击它不起作用.

Again, the link seems correct, but clicking it does not work.

截图如下:

推荐答案

您应该为文本视图启用 isSelectable 并禁用 isEditable.

You should enable isSelectable and disable isEditable for your text view.

textView.isSelectable = true
textView.isEditable = false

您还可以在文本视图的属性检查器行为部分的Interface Builder中设置这些行为:

You can also set up these behaviors inside Interface Builder in the Behavior section of the text view's Attributes inspector:

这篇关于UITextView 中的链接在 Swift 4 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆