user657819
user657819

Reputation: 141

setting an NSTextView

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

Answers (4)

M. Mayer
M. Mayer

Reputation: 133

In Swift 2.2:

myTextView.textStorage?.setAttributedString(myMutableAttributedString)

In Objective-C:

[[myTextView textStorage] setAttributedString:myMutableAttributedString];

Upvotes: 4

Jabber
Jabber

Reputation: 1

the textView is createed programmatically? if you load the NSTextView from xib,it will bi fine.

Upvotes: -2

estobbart
estobbart

Reputation: 1145

Textview must be editable to insert.

[myTextView setEditable:YES];
[myTextView insertText:myMutableAttributedString];
[myTextView setEditable:NO];

Upvotes: 14

indragie
indragie

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

Related Questions