Swift中标签内的链接

Kev*_*inB 4 hyperlink uitextview uilabel ios swift

我想在我的应用程序中这样做:

在此输入图像描述

标签如:用户名 评论

我不知道如何在标签中添加"按钮"; 我找到了这个库,但我不确定它会起作用吗?https://github.com/optonaut/ActiveLabel.swift

也许通过为第一个单词创建一个正则表达式来使用它?你怎么看?

Ahm*_*d F 9

对于这种情况,UILabel我宁愿使用UITextView,而不是将其添加为组件,因为它具有dataDetectorTypes属性:

在文本视图中转换为可点击URL的数据类型.

您可以使用此属性指定应在文本视图中自动转换为URL 的数据类型(电话号码, http链接等).点击时,文本视图会打开负责处理URL类型的应用程序并将URL传递给它.请注意, 如果文本视图的isEditable属性 设置为true,则不会发生数据检测.

因此,您可以将其实现为:

// as mentioned in the documentation, make sure to let it be uneditable:
textView.isEditable = false
textView.dataDetectorTypes = .link
Run Code Online (Sandbox Code Playgroud)

对于你的情况,我认为它应该是.link.我还建议检查UIDataDetectorTypes的其他选项.


Jac*_*ack 5

使用主动标签 https://github.com/optonaut/ActiveLabel.swift 制作一个UILabel命名的label

 let customType = ActiveType.custom(pattern: "\\sjohncena\\b") 
 label.enabledTypes.append(customType3)
 label.handleCustomTap(for: customType) { self.alert("Custom type", message: $0) }

 func alert(_ title: String, message: String) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)

其中johncena是可点击的字符串。请参阅https://github.com/optonaut/ActiveLabel.swift/blob/master/ActiveLabelDemo/ViewController.swift