慢速NSAttributed String Rendering

时间:2017-04-24 12:19:09

标签: swift string performance rendering

我正在开发一个用于文本编辑的应用程序,当我输入超过10 000时,该应用程序非常慢。我在打字时运行一些for循环,但它应该是一个大问题。是否可以在向下滚动,按块或其他内容时加载文本?像Dispatch等?我正在使用NSAttributed String

        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
    {

        textView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(0.10), range: NSMakeRange(0, textView.attributedText.length))

        let textRange: UITextRange? = textView.tokenizer.rangeEnclosingPosition((textView.selectedTextRange?.start)!, with: UITextGranularity.sentence, inDirection: 1)

        if textRange != nil
        {
            let sentenceRange = textView.rangeFromTextRange(textRange: textRange!)
            textView.textStorage.addAttribute(NSForegroundColorAttributeName, value: styles.titleTextColor().withAlphaComponent(1), range: sentenceRange)
        }

    }

    extension UITextView
{
    func rangeFromTextRange(textRange: UITextRange) -> NSRange
    {
        let location : Int = self.offset(from: self.beginningOfDocument, to: textRange.start)
        let length : Int = self.offset(from: textRange.start, to: textRange.end)

        return NSMakeRange(location, length)
    }

0 个答案:

没有答案