Re: Drawing Text with alpha
Re: Drawing Text with alpha
- Subject: Re: Drawing Text with alpha
- From: Dave Jewell <email@hidden>
- Date: Wed, 22 Aug 2007 10:53:24 +0100
On 22 Aug 2007, at 05:23, Phil wrote:
Sorry, I should have been more specific: I need the text background
to be semitransparent not completely transparent.
Hi Phil. I've fooled around with your DrawRect code. I think the
routine below will do what you want; I've tested it. As others have
said, there are two issues: you need to not specify a background
colour when drawing your string, and you need to draw the text
background separately from the text. In theory, your original code
ought to work, as far as I can see. But in practice... ;-)
Dave
- (void) drawRect: (NSRect) rect
{
// Blue background for the view
[[NSColor blueColor] set];
NSRectFill (rect);
// Build our custom attributes dictionary
NSDictionary *attr = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: [NSFont fontWithName: @"Chalkboard"
size: 18.0], [NSColor orangeColor], nil]
forKeys: [NSArray arrayWithObjects:NSFontAttributeName,
NSForegroundColorAttributeName, nil]];
// Create an attributed string and figure out its size
NSAttributedString * attrString = [[NSAttributedString alloc]
initWithString: @"Background test" attributes: attr];
NSRect textSize = [attrString boundingRectWithSize: NSMakeSize
(1e10, 1e10) options: nil];
// Draw background rect for text
[[[NSColor whiteColor] colorWithAlphaComponent: 0.1] set];
NSRectFillUsingOperation (NSMakeRect (50.0, 50.0,
textSize.size.width, textSize.size.height), NSCompositeSourceOver);
// Now draw the string itself
[attrString drawAtPoint: NSMakePoint (50.0, 50.0)];
[attrString release];
}
_______________________________________________
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