lixiang1994 / AttributedString

基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
MIT License
838 stars 76 forks source link

iOS11以下崩溃 #22

Open IAFung opened 3 years ago

IAFung commented 3 years ago

在iOS11以下的系统,kvo的监听会崩溃,An instance 0x7f9395d49750 of class SXLabel was deallocated while key value observers were still registered with it. Current observation info: 使用了textView的attachment attributed.text = .init("\(.view(customLabel, .original(.center))) \(string)", .paragraph(.lineSpacing(7)))

lixiang1994 commented 3 years ago

我换电脑了 现在没有iOS11以下的环境, 能发一些详细的截图吗? 这个问题可以稳定复现吗?

IAFung commented 3 years ago

稳定复现,使用kvo的keypath的方式在iOS11以下好像要手动调用invalidate释放 调用代码

class SXTextView: UITextView {
    private let customLabel = UILabel()
    @objc var string: String? {
        didSet {
            customLabel.text = "交易备注"
            customLabel.font = .systemFont(ofSize: 12)
            customLabel.backgroundColor = #colorLiteral(red: 0.9098039216, green: 0.2078431373, blue: 0.1333333333, alpha: 0.1004120291)
            customLabel.textColor = #colorLiteral(red: 0.9098039216, green: 0.2078431373, blue: 0.1333333333, alpha: 1)
            customLabel.textAlignment = .center
            customLabel.frame = CGRect(x: 0, y: -3, width: 58, height: 23)
            isEditable = false
            isSelectable = false
            attributed.text = .init("\(.view(customLabel, .original(.center)))    \(string!)", .paragraph(.lineSpacing(7)))
            attributed.layout()
        }
    }
    @objc func layout() {
        attributed.layout()
    }
}
let textView = SXTextView()
textView.string = "qeqewqkqwkjrqwjrkjqjnsdnfajsnfieuwrhweirhiuweqweqwe"
 view.addSubview(textView)

每次调用的页面释放时,崩溃信息如下:

2021-01-06 10:36:55.640 XibTest[14529:1236068] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7ffa900ffe00 of class XibTest.SXTextView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x618000039c20> (
<NSKeyValueObservance 0x61800005a190: Observer: 0x618000059d40, Key path: bounds, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x618000059830>
<NSKeyValueObservance 0x61800005a4c0: Observer: 0x61800005a520, Key path: frame, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x61800005a580>
)'
lixiang1994 commented 2 years ago

你好, 还存在这个问题吗? 抱歉 过这么久才看到你的回复