Re: Custom text via Core Text in a layer.
Re: Custom text via Core Text in a layer.
- Subject: Re: Custom text via Core Text in a layer.
- From: John Harper <email@hidden>
- Date: Mon, 19 Nov 2007 10:45:44 -0800
We saw something like this when implementing CATextLayer – if your
string has NSColor attributes, you need an NSGraphicsContext pointing
at the current CGContext you're drawing into, e.g. something like this:
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext
graphicsContextWithGraphicsPort:ctx flipped:NO]];
... draw text ...
[NSGraphicsContext restoreGraphicsState];
John
On Nov 19, 2007, at 9:39 AM, John Clayton wrote:
Hi All,
I'm using the following code to draw an attributed string into a
layer (my own CATextLayer), but the text *always* draws black - is
there something simple that I'm doing wrong? I am of course assuming
that the colour into in the attributed string would determine how
that content is being drawn.
Please note: most code is taken directly from the core-text API
docs :-)
// HEADER
@interface TextLayerCustom : EffectorBaseLayer
{
NSAttributedString* _string;
}
@property(readwrite, retain) NSAttributedString* string;
@end
// IMPL
@implementation TextLayerCustom
- (void) drawInContext:(CGContextRef)context
{
// core-text, here we come
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGMutablePathRef path = CGPathCreateMutable();
if(path)
{
CGPathAddRect(path, NULL, self.bounds);
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(
(CFAttributedStringRef)_string
);
// Create the frame and draw it into the graphics context
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, 0), path, NULL);
if(frame)
CTFrameDraw(frame, context);
if(framesetter)
CFRelease(framesetter);
if(frame)
CFRelease(frame);
}
if(path)
CFRelease(path);
}
- (id) init
{
self = [super init];
_string = 0;
// in order to redraw so that justification and line wrapping take
effect
[self setNeedsDisplayOnBoundsChange:YES];
return self;
}
- (void) dealloc
{
[_string release];
[super dealloc];
}
- (NSAttributedString*) string
{
return [[_string retain] autorelease];
}
- (void) setString:(NSAttributedString *)str
{
[_string release];
_string = [[NSMutableAttributedString alloc]
initWithAttributedString:str];
[self setNeedsDisplay];
}
@end
thanks!!
--
John Clayton
http://www.coderage-software.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
_______________________________________________
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