cocoa - 从 NSTextView 检索数据

标签 cocoa nstextview

我有一个可编辑的 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/

相关文章:

c++ - pthread 与 NSThread : which is faster

xcode - 如何在 swift 中向我的 cocoa 应用程序添加设置?

objective-c - 如何为 NSTextView 创建格式化工具栏?

macos - NSTextView 双视

cocoa - NSTextField 还是 NSTextView?

objective-c - PDFPage 子类上没有调用 init 吗?

objective-c - 10.8中如何显示文件复制进度

objective-c - 使用 Transforms/Security.framework 的 RSA 加密 + Objective-C

cocoa - 从 OSX lion 上的版本浏览器恢复不起作用...有什么想法吗?

objective-c - NSTextView 不会更新,除非我点击它,并且它不会聚焦。有任何想法吗?