4

我有一个 NSData 的实例,它是从我的应用程序的先前运行中存储的。我想使用这些数据设置一个 NSTextView 的文本。它是 RTF,我不知道该怎么做。有什么想法或建议吗?它需要使用代码而不是 Interface Builder 来完成。

谢谢

4

4 回答 4

9
//First convert a NSData object to a NSString object.

NSData *aData;
//assign aData to what you want it to.
NSAttributedString *aStr;
aStr = [[[NSAttributedString alloc] initWithRTF:aData documentAttributes:NULL] autorelease];
//then set the textView to that value.

[[yourTextView textStorage] setAttributedString:aStr];
于 2010-07-02T05:58:10.473 回答
2
NSData *myData; // RTF 数据
NSAttributedString *myString = [[NSAttributedString alloc] initWithRTFD:myData documentAttributes:NULL];

[[yourTextView textStorage] setAttributedString:myString];

thyrgle 很接近,但 NSAttributedString 需要使用初始化

initWithRTFD:文档属性:
代替
initWithRTF:文档属性:

于 2010-07-04T05:06:15.617 回答
0

您可以使用 RTF 数据初始化 NSAttributedString,NSTextView 拥有的 NSTextStorage 派生自 NSMutableAttributedString。我想你可以把这些碎片拼凑起来。

于 2010-07-02T05:58:11.317 回答
0

检查您是否在 textfield 或 uiview 上实现了任何类别?(您在其上实现类别的任何内置 ui)如果是,则删除该类别并再次检查。

于 2017-06-27T09:43:53.717 回答