CATextLayer with attributed string

Discussion in 'Mac Programming' started by MrFusion, May 15, 2011.

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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?

    Code:
    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
    
    
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    Oh, that is strange. I will (also) start a new project and see if it works.
    Thanks for the reply.
     

Share This Page