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 > Making a string bold?

Making a string bold?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Mar 31, 2002, 11:30 PM
 
How do I go about making a string bold or italic? NSFormatter seems to be just for numbers, dates or custom things. Is there an easy way to format strings? I just want to format the second item in bold and the last item in italics:

[referenceJournal setStringValue:[NSString stringWithFormat:@"%@, %@:%@, pp. %@", [currentReference valueForKey:@"articleJournal"],[currentReference valueForKey:@"articleVolume"],[currentReference valueForKey:@"articleIssue"],[currentReference valueForKey:@"articlePages"]]];

kman
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Apr 1, 2002, 10:22 AM
 
NSFormatter's primary purpose is the kind of formatting that means turning the number 192 into the string "$ 192.00" for use in text fields or table cells. However, it can to rich text formatting too -- for example, if you had a table of numbers being formatted as dollar amounts, you may want to have it do negative numbers in red. That's when you'd want to override -attributedStringForObjectValue:defaultAttributes: in an NSFormatter subclass.

But anyway, that doesn't seem to be what you're asking about. Looks like you've got an NSTextField whose contents you want to add text styles to, right? What you want for that is to call -setAttributedStringValue: on the text field instead of -setStringValue:. NSAttributedString is the class for manipulating rich text in Cocoa -- it's modeled as a plain string accompanied by a dictionary of information that says "this part is bold, this other part is red". See its class documentation for info on how to construct one that has all the information an formatting you want.
Rick Roe
icons.cx | weblog
     
   
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 12:29 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