Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Vertically centering text in a CATextLayer Q&A
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vertically centering text in a CATextLayer Q&A



You should use  a constraint manager; no subclassing needed.

Here's a snippet:

textLayer.layoutManager = [CAConstraintLayoutManager layoutManager];
[textLayer addConstraint:
[CAConstraint constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]];
[textLayer addConstraint:
[CAConstraint constraintWithAttribute:kCAConstraintMidY
relativeTo:@"superlayer"
attribute:kCAConstraintMidY]];
[textLayer setNeedsLayout];



On Aug 13, 2008, at 7:46 PM, David Hoerl wrote:

The question part:

CATextLayer does not offer a vertically centering option (it does offer a horizontal options). So, I thought I'd get cute and change the affine transform to translate "y" down a bit. However, nothing happens when I do this (I printed out the transforms to prove to myself it had taken.)

Surprisingly, the "scale.y" transform works as expected - the text height responds to this setting.

So, my question: is this a bug? [If so I'll enter a bug report.]

The answer part:

So, I wanted to explore and see if I could find a way to shift the text down. I finally found that if I subclassed CATextLayer, and added one method below, I could achieve my goal:

- (void)drawInContext:(CGContextRef)ctx
{
   CGFloat height, fontSize;

   height = self.bounds.size.height;
   fontSize = self.fontSize;

CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, (fontSize-height)/2.0 - 1.0); // negative
[super drawInContext:ctx];
CGContextRestoreGState(ctx);
}


Thus, including the above for the archives.


David _______________________________________________ Do not post admin requests to the list. They will be ignored. Quartz-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Quartz-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Vertically centering text in a CATextLayer Q&A (From: David Hoerl <email@hidden>)



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

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.