UILabel,然后是UIButton

时间:2018-09-12 20:14:19

标签: swift

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

我已经使用情节提要创建了UILabelUIButton

我不想使用任何Pod扩展名。

1 个答案:

答案 0 :(得分:0)

我只看到两种方式。

1。。如果您有单行UILabel,则可以使用AutoLayout将按钮放在其旁边。

2。。如果标签有或可能有多行,那么建议您使用单个UITextView而不是UILabelUIButton。设置您的文本视图,然后使用以下方法将所需的任何文本添加为​​可点击的结尾。只需确保已将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
}

希望有帮助。