【问题标题】:iOS: Adding an IBAction to part of UILabeliOS:将 IBAction 添加到 UILabel 的一部分
【发布时间】:2016-11-11 13:37:52
【问题描述】:

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

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

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

非常感谢您的帮助。

【问题讨论】:

标签: ios swift uilabel


【解决方案1】:

您应该使用 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 {
    
}

【讨论】:

  • 但这会将操作添加到所有 UILabel。有没有办法只将它添加到 UILabel 中的单词中?
  • 除非您手动渲染字体字形并跟踪非常复杂的大小和位置
【解决方案2】:

您显然不能仅在标签上进行 IBAction。为单词 email 使用单独的标签或 UIButton 并将它们彼此相邻放置,使其看起来像单个元素。

【讨论】:

    【解决方案3】:

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

    Maybe you can find the answer below

    【讨论】:

      【解决方案4】:

      我认为您不能将 IBOutlet 添加到 UILabel 的某个单词中。你可以看看ActiveLabel,它有你需要的功能。

      如果需要,这里还有一个如何打开邮件的链接:D,open mail in swift

      希望这会有所帮助,祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多