2011-03-13 54 views
7

如何将NSTextView设置为NSAttributedString?我不想使用textConainer,我试过:设置NSTextView

[myTextView insertText:myMutableAttributedString]; 

但它没有工作。什么都没有发生...甚至没有发出警告或运行时错误。任何帮助,将不胜感激。

+0

您是否验证了'myTextField'不是'nil'? – 2011-03-14 00:01:53

+0

是的,我已经验证myTextView不是零。 – user657819 2011-03-15 00:28:24

回答

3

你应该调用该方法(否则你将文本插入一个空字符串对象),然后调用-setString一个空字符串:

[myTextView setString:@""]; 
[myTextView insertText:myMutableAttributedString]; 
+0

它没有工作。甚至没有任何报告控制台中的问题。 – user657819 2011-03-16 00:36:05

14

的TextView必须是可编辑的插入。

[myTextView setEditable:YES]; 
[myTextView insertText:myMutableAttributedString]; 
[myTextView setEditable:NO]; 
+0

'insertText:'已弃用:在macOS 10.11中首先不推荐使用 - 使用-insertText:replacementRange:替换为NSTextInputClient。由于该方法仅用于输入系统,因此不应将该消息发送到应用程序的文本视图。任何内容修改都应通过NSTextStorage或NSText方法进行。 – 2017-12-12 16:03:29

-2

textView是以编程方式创建的?如果你从xib加载NSTextView,它会很好。

3

在雨燕2.2:

myTextView.textStorage?.setAttributedString(myMutableAttributedString)

在Objective-C:

[[myTextView textStorage] setAttributedString:myMutableAttributedString];