Objective c 将NSAttributedString转换为string并返回
我有NSString,我需要生成NSAttributedString NSString类似于:Objective c 将NSAttributedString转换为string并返回,objective-c,cocoa,nsstring,nsattributedstring,Objective C,Cocoa,Nsstring,Nsattributedstring,我有NSString,我需要生成NSAttributedString 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 = "Alignm
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";
}
它是UTF-8中的NSAttribute字符串。有什么方法可以做到这一点吗?您说您是从现有的
NSAttributedString
创建输入字符串的,如下所示:
[NSString stringWithFormat:@"%@", nsattributedstring]
%@
格式说明符将description
消息发送到nsattributestring
对象。description
方法的设计目的不是生成可以轻松转换回NSAttributedString
对象的字符串。它旨在帮助程序员调试他们的代码
将对象转换为字符串或字节数组的过程,以便以后可以将其转换回对象,称为序列化。使用%@
或说明
方法通常不是执行序列化的好方法。如果您真的想对description
方法创建的字符串进行反序列化,则必须编写自己的解析器。据我所知,目前还没有用于此的API
相反,Cocoa提供了一个设计用于序列化和反序列化对象的系统。使用此系统可以序列化的对象符合NSCoding
协议<代码>NSAttributedString对象符合NSCoding
。因此,请尝试通过以下方式序列化原始属性字符串:
NSMutableData *data = [NSKeyedArchiver archivedDataWithRootObject:nsattributedstring];
将数据
(非人类可读的二进制文件,而非UTF-8)保存到需要的任何位置。需要重新创建属性字符串时,请执行以下操作:
NSAttributedString *fancyText = [NSKeyedUnarchiver unarchiveObjectWithData:data];
如果您正在为OS X(而非iOS)编程,您可以选择。您可以使用(省略附件)或(包括附件)将属性化字符串转换为RTF(富文本格式),RTF是人类可读的。然后,您可以使用initWithRTF:documentAttributes:
或initWithRTFD:documentAttributes:
将RTF数据转换回属性字符串。这些方法在iOS上不可用
如果您正在为iOS 7.0或更高版本编程,则可以使用
-dataFromRange:documentAttribute:error:
或fileWrapperFromRange:documentAttribute:error:
将属性字符串转换为RTF/RTFD。您需要在文档属性中将NSDocumentTypeDocumentAttribute
设置为NSRTFTextDocumentType
或NSRTFDTextDocumentType
。使用initWithData:options:documentAttribute:error:
或initWithFileURL:options:documentAttribute:error:
将其转换回nsattributed字符串。这些方法是的一部分。您正在查找NSAttributedString*attrStr=[[NSAttributedString alloc]initWithString:@“foo”]代码>?不,我需要格式化的nsattributestring。兴趣提示。您的NSString
内容来自哪里?NSString不是UTF8。您的字符串似乎是某种文字处理器程序的输出(甚至可能是NSAttributedString的描述
输出——我从未见过这种情况),您需要理解该格式来处理它(如果它还不是属性化字符串)。Objective-C中没有执行该转换的“自动”机制。或者,执行NSLog(@“类是%s”,object_getClassName(myFunnyString))代码>,并查看打印的类名。Thx以获取答复。我知道NSCoding,但我需要获取NSAttributedString的UTF-8,因为我正在将字符串写入UTF-8xml文件。有没有办法做到这一点?@MichalJuriJurník:没有“NSAttributedString的UTF-8”这样的东西。NSAttributedString是字符串加属性;您可以将任何字符串编码为UTF-8数据,但仍然必须分别序列化属性,然后对它们进行反序列化/解码,并将它们放回另一端。使用RTF方法;它们就是为此而设计的。当我尝试时,我遇到了这个错误-如何解决它-[NSConcreteModableAttributedString\u EncodingCantbestoredineRightBitCfString]:发送到实例0xf078af0***的无法识别的选择器由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[nsConcreteModableAttributeString _encodingCantBestOredineRightBitCfString]:发送到实例0xf078af0'@Dinesh的无法识别的选择器。您需要将此作为新问题发布。包括您的代码和您尝试序列化的字符串的详细信息。