如何将NSTextView的格式化内容转换为字符串 [英] How to convert formatted content of NSTextView to string

查看:314
本文介绍了如何将NSTextView的格式化内容转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将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的格式化内容转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆