PDA

View Full Version : CATextLayer with attributed string




MrFusion
May 15, 2011, 10:06 AM
I know CATextLayer has methods for setting font and fontsize, but not for every attribution e.g. linethrough or underline. So, I am trying to set an NSAttributedString to the string property of CATextLayer.

The documentation of CATextLayer states at the beginning:
The CATextLayer provides simple text layout and rendering of plain or attributed strings. The first line is aligned to the top of the layer.
and in the discussion of the string property:
The text must be an instance of NSString or NSAttributedString. Defaults to nil.

Based on this, using NSAttributedStrings should be fine. Unfortunately, the code snippet gives "-[NSConcreteAttributedString _isCString]: unrecognized selector sent to instance". Is there another way I am supposed to create the NSAttributedString?


NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];

NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:@"strigil"
attributes:attrsDictionary] autorelease];
CATextLayer *textlayer = ...//obtain layer
textlayer.string = @"aString"; //works
textlayer.string = attrString; //does not work



jiminaus
May 15, 2011, 03:42 PM
There's nothing wrong with the posted code. I copied and pasted it into the CALayerEssentials sample code and it worked as expected without log messages.

MrFusion
May 16, 2011, 05:14 AM
There's nothing wrong with the posted code. I copied and pasted it into the CALayerEssentials sample code and it worked as expected without log messages.

Oh, that is strange. I will (also) start a new project and see if it works.
Thanks for the reply.