gpt4 book ai didi

iOS 11 : UITextView typingAttributes reset when typing

转载 作者:技术小花猫 更新时间:2023-10-29 10:35:07 30 4
gpt4 key购买 nike

我使用 typingAttributes 设置新字体。在 iOS 10 上,一切正常,但在 iOS 11 上,第一个输入的字符是正确的,但随后属性被重置为以前的,第二个字符被输入以前的字体。这是一个错误吗?我能以某种方式修复它吗?

最佳答案

为什么会这样:

自 iOS 11 以来,Apple 更新了 typingAttributes

This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text. When the text view’s selection changes, the contents of the dictionary are cleared automatically.

如何解决:

@Serdnad 的代码有效,但它会跳过第一个字符。这是我尝试所有可能想到的方法后的发现

<强>1。如果您只想为 TextView 使用一个通用类型属性

只需在此委托(delegate)方法中设置 typing 属性一次,您就可以使用这种单一的通用字体进行设置

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
//Set your typing attributes here
textView.typingAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue, NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 17)]
return true
}

enter image description here

<强>2。在我的例子中,富文本编辑器的属性一直在变化:

在这种情况下,我每次输入任何内容后都必须设置输入属性。感谢 iOS 11 的这次更新!

但是,与其在 textViewDidChange 方法中设置,不如在 shouldChangeTextIn 方法中进行设置效果更好,因为它在将字符输入 TextView 之前被调用。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.typingAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue, NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 17)]
return true
}

enter image description here

关于iOS 11 : UITextView typingAttributes reset when typing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420230/

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