gpt4 book ai didi

ios - UITextView 中的链接在 Swift 4 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:27 25 4
gpt4 key购买 nike

我使用的是 iOS 11 和 Swift 4。

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

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

这是我的代码:

@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

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

截图如下:

Screenshot of the UITextView

最佳答案

您应该为 TextView 启用 isSelectable 并禁用 isEditable

textView.isSelectable = true
textView.isEditable = false

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

Interface Builder - Attributes inspector/Behavior

关于ios - UITextView 中的链接在 Swift 4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46584071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com