UITextView为什么会闪烁?

时间:2020-09-14 09:58:51

标签: ios swift uitextview flicker

我有一个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中,并使用自动版式。如有必要,请留言,我将添加相关代码

0 个答案:

没有答案
相关问题