Re: NSShadowAttributeName doesn't scale
Re: NSShadowAttributeName doesn't scale
- Subject: Re: NSShadowAttributeName doesn't scale
- From: Graham Cox <email@hidden>
- Date: Sat, 30 Nov 2013 21:40:04 +0100
On 30 Nov 2013, at 2:54 pm, Leonardo <email@hidden> wrote:
> In facts, the NSShadow manual reports:
> "rotations, translations and other transformations of the current
> transformation matrix (the CTM) do not affect the resulting shadow."
>
> So, how to modify the NSShadowAttributeName accordingly to the view scale?
> I guess I have to modify some context's parameter in the NSTextView
> drawRect: method. But I don't know how.
That’s right: shadows don’t scale. It’s jolly annoying, but there is a way to solve it. One word of caution though, and possibly an explanation for why Apple decided to let shadows bypass the CTM - when shadows get large, they get very, very slow indeed. So much so that they will basically kill your drawing performance. Even one enlarged shadow can bring everything to a crashing halt.
So, with that out of the way, here’s the way I’ve solved this.
At the point of drawing, grab the current CTM, work out how much it *should* be scaling things, then multiply all the shadow variables by that amount, set the new shadow, and draw.
CGAffineTransform ctm = CGContextGetCTM( context );
CGSize unit = CGSizeApplyAffineTransform( CGSizeMake( 1, 1 ), ctm );
Here, ‘unit' ends up with values representing how much things are being scaled by. You then grab the original shadow parameters, multiply them by this to make a new shadow, set it, and draw.
You can do this for shadows applied to text by subclassing NSShadow and overriding the -set method, then substitute the scaled shadow and set that.
You might want to consider setting limits on what scale factors this will be allowed up to, or whether in some cases a ‘quick-and-dirty’ shadow (like drawing a solid drop shadow temporarily when doing live resizes, scrolling or zooming, replaced by a soft shadow when things settle for a second). If you don’t you will quickly see the sorts of performance tragedies I’m talking about.
—Graham
_______________________________________________
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