我需要将NSTextView的内容从Mac应用程序传输到iOS应用程序。我使用XML作为传输的文件格式。
I need transfer content of NSTextView from Mac app to iOS app. I'm using XML as transfered file format.
因此,我需要将NSTextView的内容(文本,字体,颜色atd。)保存为字符串。有什么办法怎么办?
So I need to save content of NSTextView (text, fonts, colors atd.) as a string. Is there any way how to do that?
推荐答案一种方法是归档NSAttributedString值。大纲示例代码直接键入回答:
One way to do this is to archive the NSAttributedString value. Outline sample code typed directly into answer:
NSTextView *myTextView; NSString *myFilename; ... [NSKeyedarchiver archiveRootObject:myTextStorage.textStorage toFile:myFilename];回读:
myTextView.textStorage.attributedString = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename];这就是创建和读回文件所需要的。有匹配的方法创建一个NSData而不是一个文件,你可以将NSData转换为NSString或只是插入一个NSDictionary和serialise作为plist(XML)等。
That's all that is needed to create and read back a file. There are matching methods which create an NSData rather than a file, and you can convert an NSData into an NSString or just insert one into an NSDictionary and serialise that as a plist (XML), etc.
更多推荐
如何将NSTextView的格式化内容转换为字符串
发布评论