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 > centered string within NSRect

centered string within NSRect
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2003
Status: Offline
Reply With Quote
Aug 12, 2003, 05:03 PM
 
I just started programming in Cocoa, and I can't seem to figure this one out... I want to display an NSMutableString centered within an NSRect. How do I do this? Thanks.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 12, 2003, 05:31 PM
 
Take a look at AppKit's extensions to NSString. It provides methods for getting a string's measurements and drawing the string.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Aug 12, 2003, 05:36 PM
 
You need to find the dimension of the bounding rectangle then work with that. Try something like the following:

Code:
NSRect textBounds = NSZeroRect; float middleX; textBounds.size = [label sizeWithAttributes:attributes]; // attributes should be an NSDictionary etc. middleX = textBounds.size.width/2; NSPoint labelLocation = NSMakePoint( ... ); /* ... = for some appropiate view location; when choosing the "center about point" use middleX as on offset. That is, text is rendered starting at the lower left corner of the bounding rectangle. So you need to subract middleX from the latter point. */ [label drawAtPoint:labelLocation withAttributes:attributes];
It's just one way of doing it.
(Last edited by DaGuy; Aug 12, 2003 at 05:46 PM. )
     
ewagner  (op)
Fresh-Faced Recruit
Join Date: Aug 2003
Status: Offline
Reply With Quote
Aug 12, 2003, 06:06 PM
 
Thanks DaGuy, I'll try that.

Is there also some way of doing it using NSParagraphStyle and NSTextAlignment (such as NSCenterTextAlignment)? For instance, I think I can create a string as follows:

[NSAttributedString initWithString:@"hello" attributes:DICTIONARY]

Where DICTIONARY is an NSDictionary. So, can DICTIONARY specify NSCenterTextAlignment? If so, how do I create this dictionary?
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Aug 12, 2003, 06:39 PM
 
Originally posted by ewagner:
Where DICTIONARY is an NSDictionary. So, can DICTIONARY specify NSCenterTextAlignment? If so, how do I create this dictionary?
Something like
Code:
[NSDictionary dictionaryWithObject:NSCenterTextAlignment forKey:NSTextAlignment]
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Aug 12, 2003, 06:42 PM
 
Originally posted by ewagner:
Thanks DaGuy, I'll try that.

Is there also some way of doing it using NSParagraphStyle and NSTextAlignment (such as NSCenterTextAlignment)? For instance, I think I can create a string as follows:

[NSAttributedString initWithString:@"hello" attributesICTIONARY]

Where DICTIONARY is an NSDictionary. So, can DICTIONARY specify NSCenterTextAlignment? If so, how do I create this dictionary?
Glad to be of some help. I haven't used those other classes that you are mentioning.

But in order to create the dictionary (key-value) pairs you need to know the values of the keys. These keys are described in the documentation.

Or are you just asking how to create an NSDictionary given that you already know the key-value pairs of interest? Got myself tangled... sorry.
(Last edited by DaGuy; Aug 12, 2003 at 06:50 PM. )
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 12, 2003, 07:42 PM
 
Originally posted by Angus_D:
Something like
Code:
[NSDictionary dictionaryWithObject:NSCenterTextAlignment forKey:NSTextAlignment]
That would work? You don't have to set the NSAttributedString's NSParagraphManager to have that attribute? Wow, I guess I've been doing it the hard way.

Matt Fahrenbacher
     
   
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 02:18 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