Re: CATextLayer and CAConstraint: an autoresizing height LayoutManager
Re: CATextLayer and CAConstraint: an autoresizing height LayoutManager
- Subject: Re: CATextLayer and CAConstraint: an autoresizing height LayoutManager
- From: Paul Arthur Henrion <email@hidden>
- Date: Wed, 5 Dec 2007 18:10:43 +0100
Hi all,
a couple of weeks ago I was looking for a trick to make a string fits
its CATextLayer whose width is constrained (so only the layer height
will resize).
I think a custom layout manager was the best way to do so. Here comes
my code if it can help someone:
@implementation ChangeHeightToFitTextWidth
+ (id) layoutManager
{
static ChangeFontSizeToFitTextWidth *_layoutManager;
if (nil == _layoutManager)
_layoutManager = [[[self alloc] init] retain];
return (_layoutManager);
}
- (CGSize) preferredSizeOfLayer:(CALayer *)layer
{
CGSize size = layer.bounds.size;
if (YES == ([layer isKindOfClass:[CATextLayer class]]))
{
CATextLayer *textLayer = layer;
if (YES == ([textLayer.string isKindOfClass:[NSString class]]))
{
NSDictionary *attributes = [NSDictionary
dictionaryWithObjectsAndKeys:[NSFont
fontWithName:CTFontCopyDisplayName(textLayer.font)
size:textLayer.fontSize], NSFontAttributeName, nil];
size.height = ceil([textLayer.string
sizeWithAttributes:attributes].width / (textLayer.bounds.size.width))
* textLayer.fontSize;
}
}
return (size);
}
- (void) layoutSublayersOfLayer:(CALayer *)layer
{
CGRect bounds = layer.bounds;
bounds.size = [self preferredSizeOfLayer:layer];
layer.bounds = bounds;
}
@end
So as you can see I do not support NSAttributedString yet because I
don't use them.
This layout manager is (quiet) simple, has a never-end name, doesn't
use brilliant tricks, may (surely) be improved but I find it useful :')
Best regards,
Début du message réexpédié :
De : Paul Arthur Henrion <email@hidden>
Date : 23 novembre 2007 13:21:22 HNEC
À : Scott Anguish <email@hidden>
Objet : Rép : CATextLayer and CAConstraint
Hi Scott,
as you said, my text picture has been bloody stabbed and smashed by
the mailing list -- I see it on cocoabuilder.com and I'd rather
close my eyes :-D
By the way, what I meant was: I want to constraint a CATextLayer
width with its superlayer width (via a scale factor), but I want
this CATextLayer to resize its height whenever the text to be
prompted doesn't fit, because of the width constraint.
In other words: I want my CATextLayer resizes its height but not its
width when I put a large string in it.
I think the only way to do it without subclassing CATextLayer is to
create a LayoutManager and assign it to my CATextLayer... what do
you think?
Thanks for the help, I really appreciate it -- I'm pretty new in Mac
development and I ever enjoy how Mac developers help each other.
Best regards,
Paul
Le 22 nov. 07 à 23:02, Scott Anguish a écrit :
On Nov 22, 2007, at 10:53 AM, Paul Arthur Henrion wrote:
As it's been said by Apple Documentation the CATextLayer only
automatically resizes when a default CAConstraintLayoutManager is
on.
So Aaron, instead of constraining the layer's width just set its
layoutManager to [CAConstraintLayoutManager defaultManager] and
it'll resize to fit your text.
By the way, I'd like to play with my CATextLayer's constraints,
but it does not work properly: I want my layer width to be
constraint but not its height and, whenever the string prompted in
my layer is too large, I want the layer height to increase in
order to fit the whole phrase... I don't know if you get me, my
english is not that correct (I'm Frog powered huhu).
Your English isn't the problem with this posting, your example text
"picture" was murdered by the mailing list. :-)
We have no idea what you intended for it to look like.
Maybe a small schema could help: (I'm sorry if you cannot see it
properly — font and font size issue — :'/)
+ First: ___________
This is my CATextLayer --> | Hey Apple Li | st people, how you
doin'?
¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ /\
|
This
is hidden because it does not fit the layer (too large)
+ Then it resizes as I want: ___________
This is my CATextLayer --> | Hey Apple |
| List people, |
No more hidden text
The layer width stay un- | how you |
-changed but the height | doin'? |
got bigger. ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
I've tried a couple techniques (like telling layer and / or
superlayer to layout, playing with the autoresizing mask, manually
setting the layer height, etc.) but it never does want I want.
If anyone has an idea, it would be very appreciated :')
Thanks,
Paul
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden