2014-01-27 10 views
0

CATextLayer에서 렌더링되는 기존 NSAttributedString의 색을 변경하는 범주를 만들려고합니다. 카테고리 구현은 다음과 같습니다기존 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. 이것은 심각한 오류입니다. 이 응용 프로그램 또는이 응용 프로그램이 사용하는 라이브러리는 잘못된 컨텍스트를 사용하므로 시스템 안정성과 안정성이 전반적으로 저하됩니다. 이 통지는 예의입니다.이 문제를 해결하십시오. 곧 업데이트 될 치명적인 오류가 될 것입니다.

왜 작동하지 않는가?

+0

'attributedStringWithColor :'가있는 코드를 표시하십시오. _That_는 문제가있는 곳입니다 - 표시된 코드가 아닙니다. – matt

+0

나는 그 질문을 편집했다. 감사합니다 – olynoise

+0

'textLayer.string'은 속성이 지정된 문자열입니까? 'NSString' 일 수도 있습니다. 또한 '없음'일 수도 있습니다. –

답변

0

내가 알 수있는 한, 이것은 CATextLayer의 버그이며 내 코드는 아닙니다. CATextLayers가 아닌 UILabels에 표시된 텍스트가 올바르게 작동합니다.