• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Custom text via Core Text in a layer.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Custom text via Core Text in a layer. (From: John Clayton <email@hidden>)

  • Prev by Date: Huge increase in memory usage on Leopard
  • Next by Date: Re: Custom text via Core Text in a layer.
  • Previous by thread: Custom text via Core Text in a layer.
  • Next by thread: Re: Custom text via Core Text in a layer.
  • Index(es):
    • Date
    • Thread