gpt4 book ai didi

swift - UILabel 后跟 UIButton

转载 作者:行者123 更新时间:2023-11-30 11:09:46 27 4
gpt4 key购买 nike

如何使用 Swift3 在​​ UILabel 末尾添加 UIButton,就像阅读更多一样?

我已经使用 Storyboard创建了 UILabelUIButton

我不想使用任何 Pod 扩展。

最佳答案

我认为只有两种方法可以做到这一点。

1.如果您有单行 UILabel,那么您可以使用 AutoLayout 将按钮放在它旁边。

2. 如果您的标签具有或可能具有多行,那么我建议您使用单个 UITextView 而不是 UILabel 并且UIButton。设置 TextView ,然后使用以下方法添加任何您想要的文本作为可点击的结尾。只需确保您已将 textView.isEditable 设置为 false

func addReadMore(at textView: UITextView) {
let originalString = textView.text + " "
let readMoreLine = "Read more..."
let attributedString = NSMutableAttributedString(string: originalString + readMoreLine)
let range = NSRange(location: originalString.count, length: readMoreLine.count)
attributedString.addAttribute(.link, value: "readMorePressed", range: range)
textView.attributedText = attributedString
}

然后您应该将您的 ViewController 设为 UITextView 委托(delegate)并实现以下方法。

public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluteString == "readMorePressed" {
// show more content
}
return true
}

希望有帮助。

关于swift - UILabel 后跟 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302791/

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