Re: Fill box with repeating text
Re: Fill box with repeating text
- Subject: Re: Fill box with repeating text
- From: Trygve Inda <email@hidden>
- Date: Sat, 25 Apr 2009 03:51:56 +0000
- Thread-topic: Fill box with repeating text
> There are several ways to accomplish this.
>
> (I'll note that you can get the size of an attributed string using its
> -size method.)
>
> Option 1: Create an NSImage with the size returned from the attributed
> string. Draw the attributed string into the image. Create an NSColor
> pattern from the image, using colorWithPatternImage:. Fill the rect
> using [color set] and NSRectFill()/NSRectFillUsingOperation().
>
> Option 2: you could alternatively go the slightly lower-level Core
> Graphics route, and use CGContextDrawTiledImage(), which will draw a
> tiled CGImage. I'm using this method in my personal code. I can't
> remember why at the moment, but I may have run into limitations with
> option 1. (Of course, I'd definitely try option 1 first as it'll be
> easier.)
>
> David
>
Hi David,
This is pretty close:
NSImage* theImage = [[NSImage alloc] initWithSize:[theString size]];
[theImage lockFocus];
[theString drawAtPoint:NSZeroPoint];
[theImage unlockFocus];
But I end up with black text on a white background. Adding:
NSColor* textColor = [NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0
alpha:0.50];
NSDictionary* textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
textColor, NSForegroundColorAttributeName, nil];
NSAttributedString* theString = [[NSAttributedString alloc]
initWithString:@"myText" attributes:textAttributes];
Makes the text transparent, but I can't seem to make the background of the
NSImage transparent. I want to end up with white text on a transparent
background, but [[NSImage alloc] initWithSize:[theString size]] seems to set
the background to solid white.
How can I fix this?
Thanks,
Trygve
_______________________________________________
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