gpt4 book ai didi

ios - 用户开始输入时如何记住 UITextView 中的文本属性

转载 作者:搜寻专家 更新时间:2023-11-01 06:22:12 25 4
gpt4 key购买 nike

我正在使用 UITextView 并尝试在用户键入时使用 attributedText 属性显示文本。

这是我当前的代码:

textView.attributedText = NSMutableAttributedString(
string: "",
attributes: [
NSFontAttributeName: UIFont(name: "AvenirNextLTPro-Regular", size: 12.5)!,
NSForegroundColorAttributeName: UIColor.colorFromCode(0x262626),
NSKernAttributeName: 0.5,
NSParagraphStyleAttributeName: paragraphStyle
]
)

问题是,如果您提供一个空字符串,当用户开始输入时,文本不会使用 attributedText 属性进行格式化。

但是,我注意到如果我提供一个字符串,当用户开始附加该字符串时,文本会使用 attributedText 属性进行格式化。

我能想到的完成我需要的唯一方法是重写这个函数,并在每次用户输入键时将文本设置为 attributedText:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool

没有其他更好的方法吗?

最佳答案

您需要使用 typingAttributes 属性,而不是设置一个空的属性字符串。

textView.typingAttributes = [
NSFontAttributeName: UIFont(name: "AvenirNextLTPro-Regular", size: 12.5)!,
NSForegroundColorAttributeName: UIColor.colorFromCode(0x262626),
NSKernAttributeName: 0.5,
NSParagraphStyleAttributeName: paragraphStyle
]

另见 official documentation .

The attributes to apply to new text being entered by the user.

关于ios - 用户开始输入时如何记住 UITextView 中的文本属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com