我想在段落的末尾加上"Read more“。当我点击“读取更多”文本时,应该展开它并在末尾显示"Less“。当点击“较少”文本时,文本将被折叠。

我在谷歌找到了很多样本工作。但是,我不太明白,大多数项目都是以目标C来实施的。我也在youtube上找到了。
我想知道用Swift 3实现这一点的示例代码。
我可以在不使用任何其他库的情况下实现吗?
请帮帮我。
发布于 2017-02-20 07:38:14
messageLabel的高度约束创建一个出口messageLabel获得文本的高度
func getLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat {
let lbl = UILabel(frame: .zero)
lbl.frame.size.width = width
lbl.font = font
lbl.numberOfLines = 0
lbl.text = text
lbl.sizeToFit()
return lbl.frame.size.height
}发布于 2019-08-06 09:48:38
我已经把绳子修剪好了。
我们可以通过.count比较字符串字符的长度,如果字符串中只有很少的字符,我们可以隐藏更多的按钮。
并删除修剪后的最后一个字,以确保没有可见的字被切断。然后加上“.”在最后
var trimData = ""
if eventData.eventDescription.count > 500 {
cell.readMoreLabel.isHidden = false
if !readMore {
if eventData.eventDescription.count > 500 {
trimData = String(eventData.eventDescription.prefix(500))
trimData = trimData.components(separatedBy: " ").dropLast().joined(separator: " ")
trimData = trimData+"...."
} else {
trimData = eventData.eventDescription
}
cell.readMoreLabel.attributedText = NSAttributedString(string: "Read More", attributes:
[.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
} else {
trimData = eventData.eventDescription
cell.readMoreLabel.attributedText = NSAttributedString(string: "Read Less", attributes:
[.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
}
} else {
trimData = eventData.eventDescription
cell.readMoreLabel.isHidden = true
}https://stackoverflow.com/questions/42338768
复制相似问题