将NSAttributedString保存到Parse.com

时间:2015-09-15 03:24:28

标签: ios objective-c parse-platform nsattributedstring

Parse不支持直接保存NSAttributedStrings。转换为HTML并不是最直接的。任何人都有友好的方法将NSAttributedStrings(字体和上标)存储到Parse.com?

1 个答案:

答案 0 :(得分:0)

谢谢大家,根据@Wain的评论,决定将rtf字符串保存到Parse上。

// convert NSAttributedString to RTFString and save to Parse
PFObject *note = [PFObject objectWithClassName:@"Note"];     
NSAttributedString *noteAttributedText = self.noteTextView.attributedText;
NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSRTFTextDocumentType,NSDocumentTypeDocumentAttribute, nil];
NSData *rtfData = [noteAttributedText dataFromRange:NSMakeRange(0, noteAttributedText.length) documentAttributes:documentAttributes error:NULL];
NSString *rtfString = [[NSString alloc] initWithData:rtfData encoding:NSUTF8StringEncoding];
note[@"noteTextAsRTFString"] = rtfString;

// convert RTFString to NSAttributedString after pulling from Parse
NSString *rtfString = [pfObject objectForKey:@"noteTextAsRTFString"];
NSData *data = [rtfString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *noteAttributedText = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];