我在NSTextStorage
. 我以这种方式为 textContainers 分发此文本:
let textStorageLength = defaultTextStorage?.length ?? 0
while layoutManager!.textContainer(forGlyphAt: textStorageLength - 1,
effectiveRange: nil) == nil {
let textContainer = NSTextContainer(size: textContainerSize)
layoutManager!.addTextContainer(textContainer)
pagesCount += 1
}
然后我将这个容器用于 pageViewController 上的 textViews。
我有一些标记,例如:
func selectTappableWordsFromList(_ list: [PositionWithWord]) {
self.defaultTextStorage?.beginEditing()
list.forEach {
if !self.shouldInterruptCurrentProcesses {
self.markWordInDefaultTextStorage(positionWithWord: $0)
} else {
self.shouldInterruptCurrentProcesses = false
self.defaultTextStorage?.endEditing()
return
}
}
self.defaultTextStorage?.endEditing()
}
func markWordInDefaultTextStorage(positionWithWord: PositionWithWord) {
let range = NSMakeRange(positionWithWord.position!.start,
positionWithWord.position!.length)
defaultTextStorage?.addAttributes(defaultAttributes, range: range)
}
let defaultAttributes: [NSAttributedStringKey: Any] = [
.underlineStyle: NSUnderlineStyle.styleSingle.rawValue,
.underlineColor: UIColor.BookReader.underline
]
问题是:在整个文本存储中标记单词的工作非常缓慢。书中的页数越多,运行速度就越慢。
还有来自分析器的 cpu 使用截图。
有什么方法可以提高逻辑?