Re: Vertically Centering a String within a View
Re: Vertically Centering a String within a View
- Subject: Re: Vertically Centering a String within a View
- From: Ricky Sharp <email@hidden>
- Date: Wed, 4 May 2005 21:13:23 -0500
On May 4, 2005, at 7:03 PM, August Trometer wrote:
I have an NSView subclass which I need to display messages in from
time to time. I'm displaying these messages via the drawRect:
method. I'd like to have these messages both centered horizontally
and vertically.
Horizontal centering is easy and done within the string attributes.
I can't seem to find a good way, however, to do the vertical
centering. The string attributes are set to wrap the words, and,
depending on the length of the string, the space needed can change.
It seems kind of silly that I would need to calculate the word-wrap
myself, then back-track to calculate the positioning. Is there an
easier way?
Take a look at the TipWrapper sample:
<http://developer.apple.com/samplecode/TipWrapper/TipWrapper.html>
Check out the stringWrappedForToolTipToPixelWidth: method in
NSString_Wrapping. That will properly wrap your string to the
maximum width you'd allow in your view. Then, create an attributed
string and then compute its size. Vertically center the text
bounding area as needed.
The attributes you use for your final rendering should also be used
by the code that will perform all the measuring/wrapping. Basically,
the text view that is used will need to be using the appropriate
font, etc.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden