7

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

[myTextView insertText:myMutableAttributedString];

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

4

4 回答 4

14

Textview 必须可编辑才能插入。

[myTextView setEditable:YES];
[myTextView insertText:myMutableAttributedString];
[myTextView setEditable:NO];
于 2012-09-13T00:25:34.300 回答
4

在 Swift 2.2 中:

myTextView.textStorage?.setAttributedString(myMutableAttributedString)

在 Objective-C 中:

[[myTextView textStorage] setAttributedString:myMutableAttributedString];

于 2016-10-17T17:00:55.997 回答
3

您应该-setString在调用该方法之前使用空字符串调用(否则您将文本插入到空字符串对象中):

[myTextView setString:@""];
[myTextView insertText:myMutableAttributedString];
于 2011-03-15T00:53:30.510 回答
-2

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

于 2014-03-10T03:15:53.843 回答