微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UITextView为什么会闪烁?

如何解决UITextView为什么会闪烁?

我有一个UITextView,当我执行textView.attributedText = ...时,它有一定的闪烁可能性。我不知道是什么原因引起的,但是我有一些线索:

  1. 更改attributedText时会引起闪烁。
  2. 当我更改attributedText时,它也伴随着修改contentOffset代码,因为我希望更改前后的位置保持不变。代码如下:
let oldOffset = textView.contentOffset

textView.attributedText = ...

// If it is not delayed,the modification will not take effect
dispatchQueue.main.asyncAfter(deadline: .Now() + 0.01) {
    textView.contentOffset = oldOffset
}

下面是我创建UITextView代码

public override init(frame: CGRect,textContainer: NSTextContainer? = nil) {
    super.init(frame: frame,textContainer: textContainer)
        
    config()
}

func config() {
    
    textContainerInset = UIEdgeInsets(
        top: 0,left: 10,bottom: 0,right: 10
    )

    delaysContenttouches = false
    canCancelContenttouches = true
    translatesAutoresizingMaskIntoConstraints = false
        
    backgroundColor = .clear
        
    textAlignment = .left
    isEditable = false
        
    showsverticalScrollIndicator = false
    showsHorizontalScrollIndicator = false
        
    scrollsToTop = false
    isScrollEnabled = false
    bounces = false
        
    textContainer.lineFragmentPadding = 0
    layoutManager.allowsNonContiguousLayout = false
}

我怀疑这两个原因之一是造成闪烁问题的原因,但不能排除其他原因。

如果有人可以帮助我,将不胜感激!

补充:我的UITextView嵌套在UIScrollView中,并使用自动版式。如有必要,请留言,我将添加相关代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。