UITextview设置"属性文本"导致性能降低



我是iOS的初学者。每当用户键入时,我想设置UITextview属性文本,这没关系,但问题是性能滞后,而且太慢。我不知道发生了什么。我将感谢你的任何帮助!感谢


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

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .left
let alignTextAttributes = [
NSAttributedString.Key.kern : 30,
NSAttributedString.Key.paragraphStyle : paragraphStyle,
NSAttributedString.Key.foregroundColor : UIColor.black,

]
as [NSAttributedString.Key : Any]

let atribute = NSAttributedString(string: text, attributes: alignTextAttributes)



var cursorLocation = textView.selectedRange.location
if text == ""{
textView.textStorage.replaceCharacters(in: range, with: atribute)
cursorLocation -= 1


}else{
textView.textStorage.replaceCharacters(in: range, with: atribute)
cursorLocation += 1
}

textView.selectedRange.location = cursorLocation
//add attachment image to another uitextview
let attachment = NSTextAttachment()
let imageNew = UIImage(named: "icon")
attachment.image = imageNew
let attString = NSAttributedString(attachment: attachment)
txtViewAnother.textStorage.replaceCharacters(in: range, with: attString)

return false

}

您正在做的事情可以概括为-

  1. 添加段落样式和其他文本属性
  2. 读取attributedText的旧值,用所有这些数据重新创建NSAttributedString的新实例
  3. 手动管理光标位置(向后删除(-1(与添加字符(+1((
  4. 手动更新textView.textStorage
  5. return false-不允许系统控制键入

如果您允许系统控制键入,UITextView将自动为您提供许多其他详细信息。例如

当用户选择一系列字符并点击向后删除时,您不会考虑会发生什么。(-1不适用于这种情况(。

下面的代码与您当前的代码完全相同-

// Initialize once and reuse every time
lazy var paragraphStyle: NSMutableParagraphStyle = {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .left
return paragraphStyle
}()
// Initialize once and reuse every time    
lazy var alignTextAttributes: [NSAttributedString.Key: Any] = {
return [
.kern : 30,
.paragraphStyle : paragraphStyle,
.foregroundColor : UIColor.black,
]
}()

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// If I want to set each characters with different kern values, how can I do that?
alignTextAttributes[.kern] = textView.text.count.isMultiple(of: 2) ? 30 : 10
// This is the key part
textView.typingAttributes = alignTextAttributes
// Allow system to control typing
return true
}

尝试删除光标位置更改的最后一行,看看这是否是导致性能下降的原因。如果是这样,请尝试在小延迟后执行此部分:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(5)) {[weak self] in
self?.myTextView.selectedRange.location += 1
}