1
我使用屬性字符串爲我的文本其工作正常的iOS 6和8但iOS 7我看不到字體顏色的變化。下面是我的幾個代碼塊。字體顏色不會更改爲ios7 CATextLayer
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.frame = rect;
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.truncationMode = kCATruncationEnd;
titleLayer.string = [self attributedTitleAtIndex:idx];
- (NSAttributedString *)attributedTitleAtIndex:(NSUInteger)index
{
UIColor *titleColor=[UIColor redColor];
UIColor *titleColor1 = titleAttrs[NSForegroundColorAttributeName];
UIFont *font;
if (selected) {
font = CFBridgingRelease(CFBridgingRetain([UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]));
}else {
font = CFBridgingRelease(CFBridgingRetain([UIFont fontWithName:@"HelveticaNeue" size:16.0]));
}
NSArray *arrSep = [title componentsSeparatedByString:@"("];
NSString *strSecondText = [NSString stringWithFormat:@"(%@",[arrSep objectAtIndex:1]];
NSString *strFrontText = [arrSep objectAtIndex:0];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:title];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [title length])];
[attString addAttribute:(id)kCTForegroundColorAttributeName value:titleColor1 range:NSMakeRange(0, [title length])];
// [attString addAttribute:NSForegroundColorAttributeName value:titleColor range:NSMakeRange(strFrontText.length, [strSecondText length])];
NSRange range1 = [title rangeOfString:strSecondText];
[attString addAttribute:(id)kCTForegroundColorAttributeName value:[UIColor redColor] range:range1];
// return [[NSAttributedString alloc] initWithString:(NSString *)title attributes:attString];
return attString;
}
嗨!除了你的代碼之外,你還提供了一些簡短的解釋。見https://stackoverflow.com/help/how-to-answer –
@Chirag Kalsariya我不想拿標籤。我想這樣設置字符串:titleLayer.string = [self attributedTitleAtIndex:idx]; – Rock