0

I'm trying to create a category to change the color of an existing NSAttributedString that is rendered in a CATextLayer. Here's the category implementation

@implementation NSAttributedString (_additions)

-(NSAttributedString*)attributedStringWithColor:(UIColor*)color{

    NSMutableAttributedString* mutableSelf = [self mutableCopy];

    [mutableSelf addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, mutableSelf.length)];

    return [mutableSelf attributedSubstringFromRange:NSMakeRange(0, mutableSelf.length)];

}
@end

The CATextLayer is in a subclass of CAShapeLayer. I should also mention that the CATextLayer does render with the correct attributes, EXCEPT the color.

// in .m

// class continuation:
    @interface ONCShapeLayerSubclass()
    {
        CATextLayer* textLayer;    
    }
    @end

@implementation
//...

-(void)update{

    // method that initializes the catextlayer 
    //...

    if(!textLayer){
        textLayer = [CATextLayer layer];
        textLayer.alignmentMode = kCAAlignmentCenter;
        textLayer.frame = self.bounds;
        textLayer.foregroundColor = [kDefaultLineColor CGColor];
        [self addSublayer:textLayer];
    }

    textLayer.string = //// a method that returns an attributed string from a database

   //...

    [self updateColor];
} 


-(void)updateColor{

     if(textLayer)textLayer.string = [textLayer.string attributedStringWithColor:kDefaultLineColor];
}

@end

The string shows up attributed EXCEPT for the color. And then I get a whole bunch of errors:

CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Any ideas why it isn't working?

olynoise
  • 2,016
  • 2
  • 19
  • 32
  • Show the code where you are _calling_ `attributedStringWithColor:`. _That_ is where the problem is - not in the code you have shown. – matt Jan 27 '14 at 01:36
  • Are you sure `textLayer.string` is an attributed string? It might be an `NSString`. It could also be `nil`. – Abhi Beckert Jan 27 '14 at 01:55
  • Thanks, I did check that it wasn't nil by changing to if(textLayer.string)... – olynoise Jan 27 '14 at 02:00
  • And I've checked that the strings are attributed. – olynoise Jan 27 '14 at 02:01
  • Are you really showing us the actual code where the problem actually occurs? Or are you just typing some stuff that is kind of like it? – matt Jan 27 '14 at 02:17
  • It's actual code, but its part of a big project, so I am necessarily leaving out other things that should not be relevant. I will update to include more context. – olynoise Jan 30 '14 at 22:35
  • Take a look at this http://cocoafactory.com/blog/2012/10/29/how-to-use-custom-nsattributedstring-attributes/ – sangony Jan 31 '14 at 00:54

1 Answers1

0

As far as I can tell, this is a bug with CATextLayer, and not with my code. It works correctly for text displayed in UILabels, just not in CATextLayers.

olynoise
  • 2,016
  • 2
  • 19
  • 32