 |
 |
centered string within NSRect
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2003
Status:
Offline
|
|
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
|
|
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
|
|
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.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2003
Status:
Offline
|
|
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
|
|
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
|
|
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" attributes ICTIONARY]
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
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|