【发布时间】:2021-06-19 08:25:57
【问题描述】:
我想在用户输入时设置每个字符的宽度。我使用带有键 .Kern 的 NSAttributeString 效果很好,但是当文本长度大于 1000+ 时,我触摸屏幕以将光标位置放在第一行前面并输入导致性能问题的新字符,太慢和滞后。
// 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
}
【问题讨论】:
-
对我来说这听起来像是一个设计问题。
-
@ElTomato 你是什么意思?
标签: ios swift text attributes uitextview