使用NSData在NSTextView中设置Text

bob*_*bob 4 macos xcode objective-c nstextview nsdata

我有一个NSData实例,我从之前的应用程序运行中存储.我想使用这些数据设置NSTextView的文本.它是RTF,我无法弄清楚如何做到这一点.任何想法或建议?它需要使用代码而不是Interface Builder.

谢谢

小智 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];
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复,但是当我尝试设置textview的属性字符串时,我得到了这个错误*** - [NSBigMutableString replaceCharactersInRange:withString:]:nil参数任何想法? (3认同)