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
Demi-God (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
Demi-God (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
Demi-God (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

Posting 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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Rich Text Format for emails in iOS5? applefan289 iOS 5 and earlier 3 Oct 26, 2011 06:05 PM
Issue with rich text formatting in Mail.app Ekim Neems Mac Applications and Mac App Store 0 Jul 26, 2011 08:48 AM
NSComboBox in NSTableView behaving strangely Monaj Mac Programming 0 Jul 4, 2011 10:30 AM
Showing text in selected row in NSTableView as white Monaj Mac Programming 1 Aug 31, 2010 12:14 PM
Inserting an image & text in NSTableView dharma Mac Programming 6 Feb 21, 2006 09:10 AM


All times are GMT -5. The time now is 09:28 PM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC