Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView and NSAttributedString

I'm trying to stick an attributed string into my NSTextView, but it's just showing as plain text, no attributes. I created my string like so:

    NSString *str = @"Parsing Directory Structure\n\n";

NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:str];
NSDictionary *attributes = @{
                             NSForegroundColorAttributeName : [NSColor blueColor],
                             NSFontAttributeName : [NSFont fontWithName:@"HelveticaNeue-Bold" size:20.f]
                             };
[attrstr setAttributes:attributes range:NSRangeFromString(str)];

[[self.textView textStorage] appendAttributedString:attrstr];

and on the NSTextView (inside the scrolling view) I've got the "Allows Rich Text" box checked still, and the Editable box unchecked. I'm basically trying to use this like a console output window.

like image 221
Gargoyle Avatar asked Jul 16 '13 16:07

Gargoyle


1 Answers

NSMakeRangeFromString parses a textual representation of a range, it doesn’t create a range which covers a string. As your text contains no integers it returns the range {0, 0} - location and length both zero. So therefore your text is not styled.

Replace with NSMakeRange(0, str.length) and your code should work.

like image 79
CRD Avatar answered Oct 18 '22 18:10

CRD