gpt4 book ai didi

ios - 如何在标签中添加按钮?

转载 作者:行者123 更新时间:2023-12-05 05:02:33 26 4
gpt4 key购买 nike

我需要在文本末尾添加一个按钮来触发警报。文本可以分为几行 我该怎么做?

应该是这样的 enter image description here

最佳答案

您可以使用 NSTextAttachment 在文本末尾添加 image

let fullString = NSMutableAttributedString(string: "Text")

// Create our NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "awesomeIcon.png")

// Wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)

// Add the NSTextAttachment
fullString.append(imageString)

// Draw the result in a label
yourLabel.attributedText = fullString

然后添加 UITapGestureRecognizer 以在点击后触发 Action 。

let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(showAlert(tapGesture:)))
yourLabel.addGestureRecognizer(tapGesture)

@objc func showAlert(tapGesture: UITapGestureRecognizer){
// Show alert
}

关于ios - 如何在标签中添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62149456/

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