我有一个UITextView
,当我执行textView.attributedText = ...
时,它有一定的闪烁可能性。我不知道是什么原因引起的,但是我有一些线索:
attributedText
时会引起闪烁。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
中,并使用自动版式。如有必要,请留言,我将添加相关代码