问题描述
1.png如图所示,页面上下并排显示两条文本,文本动态变化,要求两条文本上下边之间的中心点(即图中蓝色箭头所指的位置)始终位于页面的 centerY 的位置。
解决方案
根据要显示的文本,生成富文本,两条文本的间距映射为富文本的段间距。然后设置 label.centerY.equalToSuperview()
let str1 = "fakdjkaljfkldajfkdalkfda111\n"
let str2 = "abcdfefkddakghdalkgj"
let muAttStr1: NSMutableAttributedString = NSMutableAttributedString.init(string: str1)
let paragraph1 = NSMutableParagraphStyle()
paragraph1.paragraphSpacing = 10
let dic = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 20),
NSAttributedString.Key.paragraphStyle:paragraph1,
NSAttributedString.Key.backgroundColor:UIColor.red]
muAttStr1.addAttributes(dic, range: NSMakeRange(0, str1.count))
let dic2 = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)]
let muAttStr2: NSAttributedString = NSAttributedString.init(string: str2, attributes: dic2)
muAttStr1.append(muAttStr2)
desLabel.attributedText = muAttStr1.copy() as? NSAttributedString
view.addSubview(desLabel)
desLabel.snp.makeConstraints() { (make) in
make.leading.equalToSuperview().offset(10)
make.trailing.equalToSuperview().offset(-200)
make.centerY.equalToSuperview()
}
网友评论