2017-09-26 567 views

回答

5

我遇到了同样的问题,并最终通过每次编辑后再次设置typeAttributes来解决它。

斯威夫特3

func textViewDidChange(_ textView: UITextView) { 
    NotesTextView.typingAttributes = [NSForegroundColorAttributeName: UIColor.blue, NSFontAttributeName: UIFont.systemFont(ofSize: 17)] 
} 
1

为什么发生这种情况:

苹果更新typingAttributes因为iOS的11

这本词典包含的属性键(和相应的值),以适用于新输入的文字。当文本视图的选择更改时,字典的内容会自动清除。

如何解决此问题:

@ Serdnad代码的作品,但它会跳过第一个字符。这里是我的发现乱投医之后如果你只是想要一个普遍的类型属性为您的文本视图我都不可能想到的

只需在此委托方法设置的打字属性一次,你是所有设置这个单一的通用字体

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方法中执行该方法,因为它在将文字输入到文本视图之前被调用。

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

+0

我能有你的编辑器源代码 – user2296278

+0

@ user2296278可悲的是,我已经删除了编辑....它不会工作,因为由分支文本的iOS生成的HTML在Android设备上不可用 –

+0

好的,谢谢我想要这个编辑器,因为如果您重新创建或使用GitHub,或者您可以帮助我,我无法在swift 4中找到任何文本编辑器给我发消息 – user2296278