开发者

Retrieve Data From NSTextView

开发者 https://www.devze.com 2023-02-19 16:36 出处:网络
I have an Editable NSTextView,In which user will write using different font style, on certain action i need to pre开发者_如何学Pythonpare the HTML Formate of it,

I have an Editable NSTextView,In which user will write using different font style, on certain action i need to pre开发者_如何学Pythonpare the HTML Formate of it, Can anyone suggest me, how i can retrieve the data from NSTextView, I am able to use

[[pInputText textStorage] words];

but it returns NSArray, from which i am not able to get NSMutableString,

Can anyone suggest me the best possible way to retrieve the String/data what user typed and in which format.


Below solutions works for me,

- (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;
} 

Where _mString is

NSMutableAttributedString *pAttributedString = [pInputText textStorage];

NSString *pHtml  = [self convertUnderlineTextToHTML:pAttributedString];

Regards Rohan


NSTextView inherits from NSText, which has a -string method. So [pInputText string should do what you want.

Alternately, TextStorage is a subclass of NSMutableAttributedString, so if you want an attributed string you can just use the return of [pInputText textStorage] directly.


since NSTextStorage is a subclass of NSMutableAttributedString which has a method through its superclass NSAttributedString :initWithHTML:baseURL:documentAttributes, you can use this to get what you want. documentAttributes can be NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消