将NSAttributedString转换为字符串并返回

时间:2022-10-30 11:42:47

I have NSString and I need to make NSAttributedString.

我有NSString,我需要制作NSAttributedString。

NSString is something like:

NSString是这样的:

bvcx b vcxbcvx bcxvbcxv bvx xbc bcvx bxcv bcxv bcxv bcxv bcvx bcvx bcxvbcvx bvc bcvx bxcv{
NSFont = "\"LucidaGrande 24.00 pt. P [] (0x108768a80) fobj=0x108788880, spc=7.59\"";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0.05, HeaderLevel 0";
}

It's NSAttributedString in UTF-8. Is there any way how to do that?

它是UTF-8中的NSAttributedString。有什么方法可以做到吗?

1 个解决方案

#1


8  

You said you created your input string from an existing NSAttributedString like this:

你说你从现有的NSAttributedString创建输入字符串,如下所示:

[NSString stringWithFormat:@"%@", nsattributedstring]

The %@ format specifier sends the description message to the nsattributedstring object. The description method is not designed to produce a string that can be easily converted back to an NSAttributedString object. It is designed to help programmers debug their code.

%@格式说明符将描述消息发送到nsattributedstring对象。描述方法不是为了生成可以轻松转换回NSAttributedString对象的字符串而设计的。它旨在帮助程序员调试他们的代码。

The process of converting an object to a string, or an array of bytes, so that it can be converted back to an object later, is called serialization. Using %@ or the description method is generally not a good way to perform serialization. If you really want to deserialize the string created by the description method, you'll have to write your own parser. As far as I know, there is no API for that.

将对象转换为字符串或字节数组以便以后可以将其转换回对象的过程称为序列化。使用%@或描述方法通常不是执行序列化的好方法。如果您确实要对由description方法创建的字符串进行反序列化,则必须编写自己的解析器。据我所知,没有API。

Instead, Cocoa provides a system designed to serialize and deserialize objects. Objects that can be serialized using this system conform to the NSCoding protocol. NSAttributedString objects conform to NSCoding. So try serializing your original attributed string this way:

相反,Cocoa提供了一个用于序列化和反序列化对象的系统。可以使用此系统序列化的对象符合NSCoding协议。 NSAttributedString对象符合NSCoding。因此,请尝试以这种方式序列化原始属性字符串:

NSMutableData *data = [NSKeyedArchiver archivedDataWithRootObject:nsattributedstring];

Save data (which is non-human-readable binary, not UTF-8) wherever you need to. When you need to recreate the attributed string, do this:

在您需要的地方保存数据(非人类可读的二进制文件,而不是UTF-8)。当您需要重新创建属性字符串时,请执行以下操作:

NSAttributedString *fancyText = [NSKeyedUnarchiver unarchiveObjectWithData:data];

If you are programming for OS X (not iOS), you have an alternative. You can turn an attributed string into RTF (rich text format), which is fairly human-readable, using the RTFFromRange:documentAttributes: method (which omits attachments) or the RTFDFromRange:documentAttributes: method (which includes attachments). Then you can turn the RTF data back into an attributed string using initWithRTF:documentAttributes: or initWithRTFD:documentAttributes:. These methods are not available on iOS.

如果您正在为OS X(而不是iOS)编程,那么您有另一种选择。您可以使用RTFFromRange:documentAttributes:方法(省略附件)或RTFDFromRange:documentAttributes:方法(包括附件)将属性字符串转换为RTF(富文本格式),这是人类可读的。然后,您可以使用initWithRTF将RTF数据转换回属性字符串:documentAttributes:或initWithRTFD:documentAttributes:。这些方法在iOS上不可用。

If you are programming for iOS 7.0 or later, you can use -dataFromRange:documentAttributes:error: or fileWrapperFromRange:documentAttributes:error: to convert the attributed string to RTF/RTFD. You need to set NSDocumentTypeDocumentAttribute to NSRTFTextDocumentType or NSRTFDTextDocumentType in the document attributes. Use initWithData:options:documentAttributes:error: or initWithFileURL:options:documentAttributes:error: to convert back to an NSAttributedString. These methods are part of the NSAttributedString UIKit Additions.

如果您使用的是iOS 7.0或更高版本的编程,则可以使用-dataFromRange:documentAttributes:error:或fileWrapperFromRange:documentAttributes:error:将属性字符串转换为RTF / RTFD。您需要在文档属性中将NSDocumentTypeDocumentAttribute设置为NSRTFTextDocumentType或NSRTFDTextDocumentType。使用initWithData:options:documentAttributes:error:或initWithFileURL:options:documentAttributes:error:转换回NSAttributedString。这些方法是NSAttributedString UIKit Additions的一部分。

#1


8  

You said you created your input string from an existing NSAttributedString like this:

你说你从现有的NSAttributedString创建输入字符串,如下所示:

[NSString stringWithFormat:@"%@", nsattributedstring]

The %@ format specifier sends the description message to the nsattributedstring object. The description method is not designed to produce a string that can be easily converted back to an NSAttributedString object. It is designed to help programmers debug their code.

%@格式说明符将描述消息发送到nsattributedstring对象。描述方法不是为了生成可以轻松转换回NSAttributedString对象的字符串而设计的。它旨在帮助程序员调试他们的代码。

The process of converting an object to a string, or an array of bytes, so that it can be converted back to an object later, is called serialization. Using %@ or the description method is generally not a good way to perform serialization. If you really want to deserialize the string created by the description method, you'll have to write your own parser. As far as I know, there is no API for that.

将对象转换为字符串或字节数组以便以后可以将其转换回对象的过程称为序列化。使用%@或描述方法通常不是执行序列化的好方法。如果您确实要对由description方法创建的字符串进行反序列化,则必须编写自己的解析器。据我所知,没有API。

Instead, Cocoa provides a system designed to serialize and deserialize objects. Objects that can be serialized using this system conform to the NSCoding protocol. NSAttributedString objects conform to NSCoding. So try serializing your original attributed string this way:

相反,Cocoa提供了一个用于序列化和反序列化对象的系统。可以使用此系统序列化的对象符合NSCoding协议。 NSAttributedString对象符合NSCoding。因此,请尝试以这种方式序列化原始属性字符串:

NSMutableData *data = [NSKeyedArchiver archivedDataWithRootObject:nsattributedstring];

Save data (which is non-human-readable binary, not UTF-8) wherever you need to. When you need to recreate the attributed string, do this:

在您需要的地方保存数据(非人类可读的二进制文件,而不是UTF-8)。当您需要重新创建属性字符串时,请执行以下操作:

NSAttributedString *fancyText = [NSKeyedUnarchiver unarchiveObjectWithData:data];

If you are programming for OS X (not iOS), you have an alternative. You can turn an attributed string into RTF (rich text format), which is fairly human-readable, using the RTFFromRange:documentAttributes: method (which omits attachments) or the RTFDFromRange:documentAttributes: method (which includes attachments). Then you can turn the RTF data back into an attributed string using initWithRTF:documentAttributes: or initWithRTFD:documentAttributes:. These methods are not available on iOS.

如果您正在为OS X(而不是iOS)编程,那么您有另一种选择。您可以使用RTFFromRange:documentAttributes:方法(省略附件)或RTFDFromRange:documentAttributes:方法(包括附件)将属性字符串转换为RTF(富文本格式),这是人类可读的。然后,您可以使用initWithRTF将RTF数据转换回属性字符串:documentAttributes:或initWithRTFD:documentAttributes:。这些方法在iOS上不可用。

If you are programming for iOS 7.0 or later, you can use -dataFromRange:documentAttributes:error: or fileWrapperFromRange:documentAttributes:error: to convert the attributed string to RTF/RTFD. You need to set NSDocumentTypeDocumentAttribute to NSRTFTextDocumentType or NSRTFDTextDocumentType in the document attributes. Use initWithData:options:documentAttributes:error: or initWithFileURL:options:documentAttributes:error: to convert back to an NSAttributedString. These methods are part of the NSAttributedString UIKit Additions.

如果您使用的是iOS 7.0或更高版本的编程,则可以使用-dataFromRange:documentAttributes:error:或fileWrapperFromRange:documentAttributes:error:将属性字符串转换为RTF / RTFD。您需要在文档属性中将NSDocumentTypeDocumentAttribute设置为NSRTFTextDocumentType或NSRTFDTextDocumentType。使用initWithData:options:documentAttributes:error:或initWithFileURL:options:documentAttributes:error:转换回NSAttributedString。这些方法是NSAttributedString UIKit Additions的一部分。