提问人:Perzifale 提问时间:11/9/2023 更新时间:11/9/2023 访问量:36
如何优化 UITextView 以阻止滞后?
How to optimize UITextView to stop lagging?
问:
最近,我构建了一个带有 UITextView 的视图控制器。逻辑很简单,在 viewDidLoad 中有一个 API 调用来从 db 获取歌词。可以在下面找到管理 UITextView 设置的整个代码。
override func viewDidLoad() {
super.viewDidLoad()
Track(id: track!.id).getLyrics { [self] response in
guard let text = response?.lyrics_text else { return }
var writtenBy = response?.lyrics_writers ?? ""
if writtenBy.isEmpty {
writtenBy = track?.album?.artistsString() ?? ""
}
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
let attributes = [
NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 19),
NSAttributedString.Key.foregroundColor: UIColor(named: "Primary Material"),
NSAttributedString.Key.paragraphStyle: style
]
let attributedText = NSMutableAttributedString(string: writtenByPhrase+" "+writtenBy+"\n\n"+text, attributes: attributes as [NSAttributedString.Key : Any])
attributedText.addAttribute(.font, value: UIFont(name: "Helvetica-Bold", size: 19) as Any, range: NSRange(location: 0, length: writtenByPhrase.count))
textView?.attributedText = attributedText
textView?.textContainer.lineFragmentPadding = 20
textView?.textContainerInset.top = 20
textView?.textContainerInset.bottom = 200
textView?.scrollRangeToVisible(NSRange(location: 0, length: 0))
}
}
当我在真实设备上测试它并滚动浏览内容时,我想它第一次在预加载到内存时会滞后。但是,当您已经滚动浏览了所有内容时,滚动开始变得流畅。
如何设法消除第一次滚动的滞后?
答: 暂无答案
评论