import UIKit
import YYText
import SnapKit
class YYCell: UITableViewCell {
fileprivate static let fontSize: CGFloat = 24
fileprivate static let font = UIFont.systemFont(ofSize: YYCell.fontSize)
fileprivate lazy var wrapView: UIView = {
let v = UIView()
return v
}()
fileprivate lazy var textView: YYTextView = {
let tv = YYTextView()
tv.isEditable = false
tv.textContainerInset = UIEdgeInsets.zero
tv.isScrollEnabled = false
return tv
}()
fileprivate var tvHeightConstraint: Constraint? = nil
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func setupUI() {
contentView.addSubview(wrapView)
wrapView.addSubview(textView)
wrapView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
textView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(20)
make.leading.trailing.bottom.equalToSuperview()
tvHeightConstraint = make.height.equalTo(0).priority(.high).constraint
}
}
func bindData(_ n: Int) {
let text1 = NSMutableAttributedString(string: "一二三四五上山打老虎")
text1.yy_font = YYCell.font
let text2 = NSMutableAttributedString(string: "一二三四五上山打老虎结束")
text2.yy_font = YYCell.font
let attrText = NSMutableAttributedString(attributedString: text1)
for _ in 0...min(n, 50) {
for i in 0...TTSystemFaceAgent.sharedAgent.count {
if let img = TTSystemFaceAgent.sharedAgent.getUIImage(ofIndex: i) {
let att = NSMutableAttributedString.yy_attachmentString(withEmojiImage: img, fontSize: YYCell.fontSize)
attrText.append(att!)
}
}
}
attrText.append(text2)
attrText.yy_lineSpacing = 5
textView.attributedText = attrText
let size = CGSize(width: UIScreen.main.bounds.width, height: 9999999)
let layout = YYTextLayout(containerSize: size, text: attrText)
let height = ceil(layout?.textBoundingRect.height ?? 0)
_ = tvHeightConstraint?.update(offset: height)
}
}
import UIKit
import YYText
import SnapKit
class YYCell: UITableViewCell {
fileprivate static let fontSize: CGFloat = 24
fileprivate static let font = UIFont.systemFont(ofSize: YYCell.fontSize)
fileprivate lazy var wrapView: UIView = {
let v = UIView()
return v
}()
fileprivate lazy var textView: YYTextView = {
let tv = YYTextView()
tv.isEditable = false
tv.textContainerInset = UIEdgeInsets.zero
tv.isScrollEnabled = false
return tv
}()
fileprivate var tvHeightConstraint: Constraint? = nil
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func setupUI() {
contentView.addSubview(wrapView)
wrapView.addSubview(textView)
wrapView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
textView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(20)
make.leading.trailing.bottom.equalToSuperview()
tvHeightConstraint = make.height.equalTo(0).priority(.high).constraint
}
}
func bindData(_ n: Int) {
let text1 = NSMutableAttributedString(string: "一二三四五上山打老虎")
text1.yy_font = YYCell.font
let text2 = NSMutableAttributedString(string: "一二三四五上山打老虎结束")
text2.yy_font = YYCell.font
let attrText = NSMutableAttributedString(attributedString: text1)
for _ in 0...min(n, 50) {
for i in 0...TTSystemFaceAgent.sharedAgent.count {
if let img = TTSystemFaceAgent.sharedAgent.getUIImage(ofIndex: i) {
let att = NSMutableAttributedString.yy_attachmentString(withEmojiImage: img, fontSize: YYCell.fontSize)
attrText.append(att!)
}
}
}
attrText.append(text2)
attrText.yy_lineSpacing = 5
textView.attributedText = attrText
let size = CGSize(width: UIScreen.main.bounds.width, height: 9999999)
let layout = YYTextLayout(containerSize: size, text: attrText)
let height = ceil(layout?.textBoundingRect.height ?? 0)
_ = tvHeightConstraint?.update(offset: height)
}
}
import UIKit import YYText import SnapKit class YYCell: UITableViewCell { fileprivate static let fontSize: CGFloat = 24 fileprivate static let font = UIFont.systemFont(ofSize: YYCell.fontSize) fileprivate lazy var wrapView: UIView = { let v = UIView() return v }() fileprivate lazy var textView: YYTextView = { let tv = YYTextView() tv.isEditable = false tv.textContainerInset = UIEdgeInsets.zero tv.isScrollEnabled = false return tv }() fileprivate var tvHeightConstraint: Constraint? = nil override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupUI() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } fileprivate func setupUI() { contentView.addSubview(wrapView) wrapView.addSubview(textView) wrapView.snp.makeConstraints { (make) in make.edges.equalToSuperview() } textView.snp.makeConstraints { (make) in make.top.equalToSuperview().offset(20) make.leading.trailing.bottom.equalToSuperview() tvHeightConstraint = make.height.equalTo(0).priority(.high).constraint } } func bindData(_ n: Int) { let text1 = NSMutableAttributedString(string: "一二三四五上山打老虎") text1.yy_font = YYCell.font let text2 = NSMutableAttributedString(string: "一二三四五上山打老虎结束") text2.yy_font = YYCell.font let attrText = NSMutableAttributedString(attributedString: text1) for _ in 0...min(n, 50) { for i in 0...TTSystemFaceAgent.sharedAgent.count { if let img = TTSystemFaceAgent.sharedAgent.getUIImage(ofIndex: i) { let att = NSMutableAttributedString.yy_attachmentString(withEmojiImage: img, fontSize: YYCell.fontSize) attrText.append(att!) } } } attrText.append(text2) attrText.yy_lineSpacing = 5 textView.attributedText = attrText let size = CGSize(width: UIScreen.main.bounds.width, height: 9999999) let layout = YYTextLayout(containerSize: size, text: attrText) let height = ceil(layout?.textBoundingRect.height ?? 0) _ = tvHeightConstraint?.update(offset: height) } }
主要是YYTextView不支持Autolayout,需要搞一个YYTextLayout算一下,然后再更新autolayout约束。另外为了避免冲突,搞了个优先级。