Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform an NSAttributedString to plain text

I have an instance of NSData containing attributed text (NSAttributedString) originating from an NSTextView. I want to convert the attributed string to a plain string (NSString) without any formatting to do some text analysis (at the moment of conversion I do not have access to the originating NSTextView nor its NSTextStorage instance).

What would be the best way to do this?

EDIT:

Out of curiosity I examined the result of:

[[[self textView] textStorage] words] 

which appeared to be a handy thing for doing some text analysis. The resulting array contains instances of NSSubTextStorage (example below of the word "Eastern"):

Eastern{ NSFont = "\"LucidaGrande 11.00 pt. P [] (0x7ffcaae08330) fobj=0x10a8472d0, spc=3.48\""; NSParagraphStyle = "Alignment 0, 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"; }

NSSubTextStorage is probably a private class as I could not find any documentation for it. It also retains all formatting.

like image 240
Roger Avatar asked Jan 26 '12 12:01

Roger


People also ask

What is an NSAttributedString?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

What is NSMutableAttributedString in Swift?

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.

How do I create an attributed string in swift 5?

First create an NSMutableAttributedString with a new font attribute. You don't use textView. text . Then append another attributed string that doesn't have any attributes set.


2 Answers

If I understand you correctly you have an NSData, say data, containing an encoded NSAttributedString. To reverse the process:

NSAttributedString *nas = [[NSAttributedString alloc] initWithData:data                                                            options:nil                                                 documentAttributes:NULL                                                              error:NULL]; 

and to get the plain text without attributes you then do:

NSString *str = [nas string]; 
like image 173
CRD Avatar answered Oct 08 '22 03:10

CRD


Updating for Swift 5:

attributedText.string 
like image 44
Justin Vallely Avatar answered Oct 08 '22 03:10

Justin Vallely