简体   繁体   English

使用NSData在NSTextView中设置Text

[英]Set Text in NSTextView with NSData

I have an instance of NSData that I stored from a previous run of my application. 我有一个NSData实例,我从之前的应用程序运行中存储。 I want to set an NSTextView's text using this data. 我想使用这些数据设置NSTextView的文本。 It is RTF and I can't figure out how to do this. 它是RTF,我无法弄清楚如何做到这一点。 Any ideas or suggestions? 任何想法或建议? It needs to be done with code and not Interface Builder. 它需要使用代码而不是Interface Builder。

Thanks 谢谢

//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];
NSData *myData; // RTF data
NSAttributedString *myString = [[NSAttributedString alloc] initWithRTFD:myData documentAttributes:NULL];

[[yourTextView textStorage] setAttributedString:myString];

thyrgle is close, but NSAttributedString needs to be initialized using thyrgle很接近,但NSAttributedString需要使用初始化

initWithRTFD: documentAttributes:
instead of 代替
 initWithRTF: documentAttributes: initWithRTF:documentAttributes: 

You can initialize an NSAttributedString using RTF data, and the NSTextStorage owned by the NSTextView derives from NSMutableAttributedString. 您可以使用RTF数据初始化NSAttributedString,NSTextView拥有的NSTextStorage来自NSMutableAttributedString。 I think you can put the pieces together from that. 我想你可以把它们拼凑起来。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM