Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vertically Centering a String within a View




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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Vertically Centering a String within a View (From: August Trometer <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.