如何将NSTextView的格式化内容转换为字符串

编程入门 行业动态 更新时间:2024-10-24 07:26:52
本文介绍了如何将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的格式化内容转换为字符串

本文发布于:2023-11-16 17:05:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   如何将   内容   NSTextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!