// MARK: - TextView 高度自适应 final class BaseAutoResizeTextView: UITextView { var maxHeight = CGFloat.greatestFiniteMagnitude override var contentSize: CGSize { get { super.contentSize } set { invalidateIntrinsicContentSize() super.contentSize = newValue } } override var intrinsicContentSize: CGSize { guard contentSize.height <= maxHeight else { var size = contentSize size.height = maxHeight return size } return contentSize } }
使用
let contentTextView = BaseAutoResizeTextView() contentTextView.textContainerInset = .zero contentTextView.isEditable = false contentTextView.isSelectable = false contentTextView.backgroundColor(.clear) contentTextView.font = UIFont.yd.textRegular contentTextView.maxHeight = 231 contentTextView.textColor = R.color.text_sub() contentTextView.textAlignment = .left contentTextView.text = """ 1.为了更好的产品体验,我们会收集相关设备权限,例如定位XXX 2.我们接入了推送XXX,向您提供占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 3.占位占位占位占位占位占位占位占位占位占位占位占位 """
contentTextView.snp.makeConstraints { make in make.left.right.equalTo(subLab) make.top.equalTo(subLab.snp.bottom).offset(16) }