如何使用Swift3在UIButton
的末尾添加UILabel
,就像阅读更多一样?
我已经使用情节提要创建了UILabel
和UIButton
。
我不想使用任何Pod扩展名。
答案 0 :(得分:0)
我只看到两种方式。
1。。如果您有单行UILabel
,则可以使用AutoLayout
将按钮放在其旁边。
2。。如果标签有或可能有多行,那么建议您使用单个UITextView
而不是UILabel
和UIButton
。设置您的文本视图,然后使用以下方法将所需的任何文本添加为可点击的结尾。只需确保已将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
的代表,并实现以下方法。
public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluteString == "readMorePressed" {
// show more content
}
return true
}
希望有帮助。