On Wed, Jul 20, 2011 at 4:12 PM, Fritz Anderson <fri...@manoverboard.org>
 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to