Re: Drawing text with alpha
Re: Drawing text with alpha
- Subject: Re: Drawing text with alpha
- From: Phil <email@hidden>
- Date: Wed, 22 Aug 2007 01:24:26 -0400
I appreciate the pointers. I'll likely go with your approach as I.S.
is also recommending it and no one has indicated that it's possible
to get NSAttributedString to do it directly. I wanted to make sure
I wasn't missing something and it looks like I wasn't.
On a related note, am I correct in assuming that to get
semitransparent text I'd need to render it to an image and have
Quartz handle the compositing?
Thanks,
Phil
On Aug 21, 2007, at 9:17 PM, Ricky Sharp wrote:
On Aug 21, 2007, at 8:06 PM, I. Savant wrote:
On Aug 21, 2007, at 8:41 PM, Phil wrote:
This seems to be a common question related to drawing graphics
but I haven't found an answer that addresses alpha and text.
I've got a simple NSView subclass which draws a blue background
and want to draw text with it's background alpha set to .1. But
as the alpha is decreased, the background becomes black. My
example (how not to do it) code:
Maybe I'm not reading very carefully (it's late), but have you
tried *not* specifying the background of your text? In other
words, don't specify a color for the NSBackgoundColorAttributeName.
I also had trouble getting the code to work as the OP wanted (I'll
blame it on being late here too :)
Anyhow, I know that a workaround would be to do the following:
1) As I.S. states, do not apply any background color to the text.
2) Build an attributed string and obtain its size
3) Create an NSRect with your origin and string's size, set the
current context's color to your 0.1 alpha white color, and then use
NSRectFillUsingOperation (yourRect, NSCompositeSourceOver).
4) Draw your attributed string into yourRect.
One thing I did try in the original code was to bracket the string
drawing with:
NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
[theContext saveGraphicsState];
[theContext setCompositingOperation:NSCompositeSourceOver];
... draw string
[theContext restoreGraphicsState];
I figured that string rendering may honor the compositing
operation, but it did not.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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