我有一个可编辑的 NSTextView,用户将在其中使用不同的字体样式进行编写,在某些操作中我需要准备它的 HTML 格式, 谁能建议我,如何从 NSTextView 检索数据, 我能够使用
[[pInputText textStorage] words];
但它返回 NSArray,我无法从中获取 NSMutableString,
任何人都可以建议我检索用户输入的字符串/数据和格式的最佳方法。
最佳答案
以下解决方案对我有用,
- (NSString *)convertUnderlineTextToHTML:(NSAttributedString *)_mString
{
NSArray * exclude = [NSArray arrayWithObjects:@"doctype", @"html",
@"head", @"body",@"xml",nil];
NSDictionary * htmlAtt = [NSDictionary
dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType,NSDocumentTypeDocumentAttribute,exclude,NSExcludedElementsDocumentAttribute,nil];
NSError * error;
NSData * htmlData = [_mString dataFromRange:NSMakeRange(0, [_mString
length]) documentAttributes:htmlAtt error:&error];
NSString * sdat = [[NSString alloc] initWithData:htmlData
encoding:NSUTF8StringEncoding];
NSLog(sdat);
return sdat;
}
_mString 在哪里
NSMutableAttributedString *pAttributedString = [pInputText textStorage];
NSString *pHtml = [self convertUnderlineTextToHTML:pAttributedString];
问候 罗汉
关于cocoa - 从 NSTextView 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418741/