iOS: 将 IBAction 添加到 UILabel 的一部分

iOS: Adding an IBAction to part of UILabel

我有 UILabel,我想在单词 "email" 中添加 IBAction 以在 iPhone 中打开电子邮件客户端。这是我的代码:

func setInfoLabel() {
    self.myLabel.text = "send me and email if you need more information"
}

你们中有人知道如何将动作添加到 UILabel 中的单词吗?

非常感谢你的帮助。

你应该使用 UIButton 而不是 UILabel 因为它只是为了显示文本而创建的,但是,如果你仍然想使用它

override func viewDidLoad() {
    super.viewDidLoad()

    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
    self.myLabel.addGestureRecognizer(gestureRecognizer)
} 

/* will be called when tapping on the label */
@objc func tapAction() -> Void {
    
}

您显然不能只对标签的一部分进行 IBAction。为单词 email 使用单独的标签或 UIButton,并将它们彼此紧挨着放置,使其看起来像一个元素。

您应该使用属性文本在一个句子中指定要链接的区域。

Maybe you can find the answer below

我认为您不能为 UILabel 的某个词添加 IBOutlet。您可以查看 ActiveLabel,它具有您需要的功能。

这里还有 link 如何在需要时打开邮件:D, open mail in swift.

希望这对您有所帮助,祝您好运。