user1927992
user1927992

Reputation: 123

Cocoa Changing properties of NSTextView

I have an NSTextView object in my window. I have 2 problems that I need to solve.

1) My NSTextView content changes between different NSStrings that I have set. I would like to make the text a bit bigger but when the app initialize, there will be no content there.How do I set it that all the Strings passed to it can make their size bigger. Thanks

2) I don't want my content editable by the user but when I uncheck "Editable" in the inspector, I wouldn't be able to change the content of the NSTextView myself in the code. How do I make it so that the content may change but the content remains unediable by the user? Thanks!

Upvotes: 0

Views: 558

Answers (1)

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

Use instances of NSAttributedString to set the content of your text view. You can set up attributed strings so that they use a particular font size. When you do this, do it in your app controller's -awakeFromNib method to address the issue of there being no content when your application starts up.

You can still change the content of your text view, even if you reset the "Editable" flag. That only prohibits your users from changing the content. Have you actually tried changing the content programmatically?

EDIT

What follows is an example of using an attributed string. Apple's documentation on the subject covers it very nicely. If you're not using ARC, be sure to release attrString when you're done with it (or autorelease it).

NSFont *font = [NSFont fontWithName:@"Helvetica" size:24.0];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"this is Helvetica at 24 points" attributes:attributes];

[[[self textView] textStorage] appendAttributedString:attrString];

Upvotes: 1

Related Questions