Reputation: 141
How can I set an NSTextView
to an NSAttributedString
? I do not want to use a textConainer, and I tried:
[myTextView insertText:myMutableAttributedString];
but it didn't work. Nothing happened...not even a warning or a runtime error. Any help would be appreciated.
Upvotes: 7
Views: 4545
Reputation: 133
In Swift 2.2:
myTextView.textStorage?.setAttributedString(myMutableAttributedString)
In Objective-C:
[[myTextView textStorage] setAttributedString:myMutableAttributedString];
Upvotes: 4
Reputation: 1
the textView is createed programmatically? if you load the NSTextView from xib,it will bi fine.
Upvotes: -2
Reputation: 1145
Textview must be editable to insert.
[myTextView setEditable:YES];
[myTextView insertText:myMutableAttributedString];
[myTextView setEditable:NO];
Upvotes: 14
Reputation: 18132
You should call -setString
with an empty string before calling that method (otherwise you're inserting text into a null string object):
[myTextView setString:@""];
[myTextView insertText:myMutableAttributedString];
Upvotes: 3