image.png
1.创建文字
lazy var signatureLabel: YYLabel = {
let new = YYLabel()
new.text = "请编辑您的个性签名,获得更大的曝光"
new.font = UIFont.systemFont(ofSize: 14)
new.textColor = ColorSubtitle
new.textAlignment = .left
new.textVerticalAlignment = .top
new.numberOfLines = 0
new.backgroundColor = .systemYellow
return new
}()
2.添加全文
func addOpenButton() {
Logger.log("添加全文")
self.signatureLabel.numberOfLines = 3
let text = NSMutableAttributedString(string: "...全文")
let hi = YYTextHighlight()
hi.setColor(ColorTheme)
hi.tapAction = { containerView, text, range, rect in
self.isExpand = !self.isExpand
if let block = self.openLabelBlock {
block()
}
}
text.yy_font = UIFont.boldSystemFont(ofSize: 14)
text.yy_setColor(ColorBlue, range: NSRange(location: 3, length: 2))
text.yy_setTextHighlight(hi, range: NSRange(location: 3, length: 2))
let seeMore = YYLabel()
seeMore.attributedText = text
seeMore.sizeToFit()
//设置断点位置
let truncationToken = NSAttributedString.yy_attachmentString(withContent: seeMore, contentMode: .top, attachmentSize: CGSize(width: 60, height: 16), alignTo: UIFont.boldSystemFont(ofSize: 14), alignment: YYTextVerticalAlignment.top)
//当label空间不够展示时,添加断点的全文按钮
self.signatureLabel.truncationToken = truncationToken
}
3.添加收起
func addPackupButton() {
Logger.log("添加收起")
self.signatureLabel.numberOfLines = 0
let text = NSMutableAttributedString(string: "收起")
let hi = YYTextHighlight()
hi.setColor(ColorTheme)
hi.tapAction = { containerView, text, range, rect in
self.isExpand = !self.isExpand
if let block = self.openLabelBlock {
block()
}
}
text.yy_font = UIFont.boldSystemFont(ofSize: 14)
text.yy_setColor(ColorBlue, range: NSRange(location: 0, length: 2))
text.yy_setTextHighlight(hi, range: NSRange(location: 0, length: 2))
let attributes = self.getAttributes(signatureString)
attributes.append(text)
self.signatureLabel.attributedText = attributes
}
4.计算行数高度等,可自行实现
func getAttributes(_ content: String) -> NSMutableAttributedString {
let attributes = NSMutableAttributedString(string: content)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
attributes.addAttributes([ .paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: attributes.length))
return attributes
}
5.判断是否展开,可自行实现
self.signatureLabel.attributedText = self.getAttributes(signatureString)
//计算行数
var attH = ComUitl.getStringHeight(signatureString, kScreenWidth-40)
var rows = ceil(attH/14)
//控制全文/收起
if rows > 3 {
Logger.log(isExpand)
self.setExpand(isExpand)
}
//控制高度
var H = attH
if self.isExpand == false && rows > 3 {
H = 3 * 20
}else{
//当3行以上,需要在句末加上“收起”占位
let tempString = signatureString + "收起"
attH = ComUitl.getStringHeight(tempString, kScreenWidth-40)
rows = ceil(attH/14)
H = rows * 20 + 10
}
Logger.log("行数\(rows)纯文本高度\(attH)实际高度\(H)")
signatureLabel.snp.updateConstraints { (make) in
make.height.equalTo(H)
}
6.其他
var isExpand: Bool = false
let signatureString = "水天相接,晨雾蒙蒙笼云涛。银河转动,像无数的船只在舞动风帆。梦魂仿佛回天庭,听见天帝在对我说话。我回报天帝路途还很漫长,现在已是黄昏却还未到达。即使我学诗能写出惊人的句子,又有什么用呢?长空九万里,大鹏冲天飞正高。风啊!千万别停息,将我这一叶轻舟,直送往蓬莱三仙岛哈哈。"
var openLabelBlock: OnListenBlock?
typealias OnListenBlock = () -> ()
signatureLabel.snp.makeConstraints { (make) in
make.left.equalTo(20)
make.right.equalTo(-20)
make.top.equalTo(titleLabel.snp.bottom).offset(13)
make.height.equalTo(180)
make.bottom.equalTo(-16)
}
注意:label需要设置高度或frame,当空间不够时,才添加更多
网友评论