Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTextView setString for NSAttributedString?

NSTextView setString for NSAttributedString?
Thread Tools
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Nov 20, 2002, 10:14 PM
 
I want to place an NSAttributedString into an NSTextView, in a similar way to what the setString method does with NSString.

However, there does not appear to be any setAttributedString method for NSTextView.

I can use the insertText method, which works, but that is not quite the same thing, as it requires all existing text to be selected to have the same result, and it also calls the delegate method, textDidChange, which I definitely DON'T want.

How should I set the contents of and NSTextView to be the value of an NSAttributedString I already have?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Nov 20, 2002, 10:23 PM
 
Do this:

Code:
@interface NSTextView (MyAdditions) - (void)insertAttributedString:(NSAttributedString *)str silent:(BOOL)silent; @end @implementation NSTextView (MyAdditions) - (void)insertAttributedString:(NSAttributedString *)str silent:(BOOL)silent { id delegate = [[self delegate] retain]; if(silent) [self setDelegate:nil]; [self setString:@""]; [self insertText:str]; if(silent) [self setDelegate:delegate]; [delegate autorelease]; } @end

I think this catagory will work for you, although I typed it straight into Chimera, so who knows.

HTH,
Matt
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Nov 20, 2002, 10:29 PM
 
Thanks for the demo code. I think that should work. It's annoying that there is no such method already built in, as I would have thought it would be a fairly common operation.

I'll give your code a try. It looks a little nicer than the work around I was thinking about.
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Nov 21, 2002, 04:50 PM
 
DONE!

I made some minor alterations: I didn't use the silent BOOL (and always removed the delegate temporarily); I named the method "setAttributedString"; I coded it for NSText instead of NSTextView. All of these changes make it more similar to the "setString" method that already exists (for NSStrings) in the NSText class.

It works very nicely - thank you for your help.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2