CATextLayer でレンダリングされる既存の NSAttributedString の色を変更するカテゴリを作成しようとしています。これがカテゴリの実装です
@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
CATextLayer は CAShapeLayer のサブクラスです。また、CATextLayer は色を除いて正しい属性でレンダリングされることにも言及する必要があります。
// 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
文字列は、色の EXCEPT 属性で表示されます。そして、私はたくさんのエラーを受け取ります:
CGContextSetFillColorWithColor: 無効なコンテキスト 0x0。これは重大なエラーです。このアプリケーション、またはアプリケーションが使用するライブラリは無効なコンテキストを使用しているため、システムの安定性と信頼性が全体的に低下しています。この通知は厚意によるものです。この問題を修正してください。今後のアップデートで致命的なエラーになります。
それが機能しない理由はありますか?