• 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
Custom text via Core Text in a layer.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Custom text via Core Text in a layer.


  • Subject: Custom text via Core Text in a layer.
  • From: John Clayton <email@hidden>
  • Date: Mon, 19 Nov 2007 18:39:07 +0100

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


  • Follow-Ups:
    • Re: Custom text via Core Text in a layer.
      • From: John Harper <email@hidden>
  • Prev by Date: Activate menubar menu programatically?
  • Next by Date: CTFrame and drawing text + layers.
  • Previous by thread: Re: Activate menubar menu programatically?
  • Next by thread: Re: Custom text via Core Text in a layer.
  • Index(es):
    • Date
    • Thread