带有嵌入在UITableViewCell滚动滞后中的HTML属性字符串的iOS 13 UITextView

时间:2019-12-17 04:03:35

标签: ios swift uitableview uitextview nsmutableattributedstring

在IOS 12中,即使html字符串包含图像,UITableView的滚动也很平滑,没有滞后,但是在IOS 13中,UITableView的滚动变得迟钝。已经尝试shouldresterizing = true,但不能解决问题。模拟器和设备的结果相同。

class ProductTextViewCell:UITableViewCell {

    @IBOutlet weak var textView: UITextView!

    override func awakeFromNib() {
        super.awakeFromNib()

        textView.textContainer.lineFragmentPadding = 0
        textView.text = "-"
        textView.dataDetectorTypes = .all
    }
    func set(str:String) {
        print(str)

        if let attributed = str.html2AttributedString {
            let mutable = NSMutableAttributedString(attributedString: attributed)
            self.textView.attributedText = mutable

            mutable.enumerateAttribute(NSAttributedStringKey.attachment, in: NSMakeRange(0, attributed.length), options: .init(rawValue: 0), using: { (value, range, stop) in

                if let attachement = value as? NSTextAttachment {
                    if let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location) {
                        let screenSize: CGRect = UIScreen.main.bounds

                        let max = screenSize.width - 20
                        print(max)
                        print(image.size.width)
                        if image.size.width > max {
                            let scale = image.size.height / image.size.width
                            attachement.bounds = CGRect(x: 0, y: 0, width: max, height: max * scale)
                        }
                    }
                }
            })
        } else {
            self.textView.attributedText = nil
        }
    }
}

0 个答案:

没有答案