Re: Smooth resizing of a text field
Re: Smooth resizing of a text field
- Subject: Re: Smooth resizing of a text field
- From: Gabriel Roth <email@hidden>
- Date: Wed, 20 Jul 2011 21:13:05 -0400
On Wed, Jul 20, 2011 at 4:12 PM, Fritz Anderson <email@hidden>
wrote:
>
> A couple of minutes' search turned up drawCenteredShrinkingToFitInRect: in
> NSAttributedString-OAExtensions. I don't think it's on-point for what you're
> looking for, but you may be able to derive a technique.
To report back: With some minor simplifications, the method Fritz pointed me
to in the OmniGroup frameworks serves my purpose very well. Here's what I
added as a category on NSAttributedString:
- (void)drawCenteredShrinkingToFitInRect:(NSRect)rect
{
CGFloat scale = MIN(rect.size.width / self.size.width, rect.size.height
/ self.size.height);
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:rect.origin.x yBy:rect.origin.y];
[transform scaleBy:scale];
[[NSGraphicsContext currentContext] saveGraphicsState];
[transform concat];
rect.origin = NSZeroPoint;
rect.size.width /= scale;
rect.size.height /= scale;
[self drawInRect:rect];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
I'm calling it from the drawRect: method of a custom view. As the view
resizes, the text grows and shrinks smoothly. I will confess that my
understanding of how this method works is imcomplete, but I'd much rather
have working code and try to figure out what it's doing than not know how to
get it working.
Duncan's email arrived as I was finishing, so I haven't investigated that
approach, but I can see how Core Animation would also be suitable. Many
thanks to both of you.
gr.
_______________________________________________
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