Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 2, 2007, 01:47 AM   #1
Oats
macrumors regular
 
Join Date: Jan 2003
Location: New York
formatting rich text (fonts, colors) in NSTableView

I have an NSTableView with several entries, and I want to highlight certain words in the table. For example, every time the word "bar" appears in the table, I want that word to be bold and colored red, as in the example line below:

"foobar foo foo bar foo"

Any idea how I can accomplish this? Basically, I want to use rich text in the NSTableView cells. I was thinking of using the NSText method replaceCharactersInRange:withRTFD: but I really have no idea if this is correct. I would still need to figure out the RTF syntax, but thats a different story. Please help?
Thanks!
Oats is offline   0 Reply With Quote
Old Aug 2, 2007, 04:23 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
NSAttributedString allows you to add formatting to string objects. Most Cocoa cells and views will respect this when drawing, standard text cells do.
robbieduncan is offline   0 Reply With Quote
Old Aug 2, 2007, 08:19 AM   #3
Oats
Thread Starter
macrumors regular
 
Join Date: Jan 2003
Location: New York
Thanks! Once I have formatted a string, can I just use the (NSAttributedString *) instead of the (NSString *) when initializing the NSTableView dictionary?
Oats is offline   0 Reply With Quote
Old Aug 2, 2007, 08:21 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Oats View Post
Thanks! Once I have formatted a string, can I just use the (NSAttributedString *) instead of the (NSString *) when initializing the NSTableView dictionary?
I'm not sure what you mean by that. Could you post the code? NSTableViews don't have dictionaries
robbieduncan is offline   0 Reply With Quote
Old Aug 2, 2007, 08:47 AM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
I assume you mean the table content, and the answer to that is yes.
Eraserhead is offline   0 Reply With Quote
Old Aug 2, 2007, 06:35 PM   #6
Oats
Thread Starter
macrumors regular
 
Join Date: Jan 2003
Location: New York
Quote:
Originally Posted by robbieduncan View Post
I'm not sure what you mean by that. Could you post the code? NSTableViews don't have dictionaries
i am obviously new to this, my dictionary comment was referring to a NSMutableDictionary, which i believe has the contents binding to the NSTableView. as it turns out, i was able to just use a NSAttributedString in place of the NSString, and this works! the sample code here is probably convoluted... i know HTML, not RTF, so this seemed the easiest way to format my string?


PHP Code:
NSArray keys      = [NSArray arrayWithObjects: @"linenum", @"linestr"nil];
//NSArray * values    = [NSArray arrayWithObjects: linenum, linestr, nil];
NSString theHTML = @"<font face='Monaco' size='3'>foo foo foo<b><i><font color=#EE0000>bar</font></i></b> fooobar</font>";
// fill the NSData buffer with the contents of the NSString
NSData htmlData = [theHTML dataUsingEncodingNSUTF8StringEncoding];
NSAttributedString styledText = [[NSAttributedString alloc]
                
initWithHTMLhtmlData  documentAttributesnil]; 
NSArray values    = [NSArray arrayWithObjectslinenumstyledTextnil];
properties = [[NSMutableDictionary allocinitWithObjectsvalues forKeyskeys]; 
Oats is offline   0 Reply With Quote
Old Aug 3, 2007, 01:46 AM   #7
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Easier != better.

Personally, I'd just build the attributed string manually, using initWithString:attributes:, and then combine the different attributed strings together to make the final attributed string (see NSMutableAttributedString). To me, that seems like the right "Cocoa way".
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Textograph - Rich text editing capable beautiful Photography app sleepwalkerfx iPhone and iPod touch Apps 10 Oct 9, 2013 11:49 PM
Resolved: Default Mail Fonts & Colors DarkNetworks Mac Basics and Help 2 Oct 19, 2012 11:01 AM
Rich text in Notes? pk7 iOS 5 and earlier 2 Sep 26, 2012 09:54 AM
Chrome 22 - lighter fonts/colors? kbmb Mac Applications and Mac App Store 2 Sep 25, 2012 06:05 PM

Forum Jump

All times are GMT -5. The time now is 04:23 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC