Objective c ios7 CATextLayer的字体颜色不会更改
我正在为我的文本使用属性字符串,它在iOS 6和iOS 8中运行良好,但在iOS 7中,我看不到字体颜色的变化。下面是我的几段代码Objective c ios7 CATextLayer的字体颜色不会更改,objective-c,ios7,nsattributedstring,Objective C,Ios7,Nsattributedstring,我正在为我的文本使用属性字符串,它在iOS 6和iOS 8中运行良好,但在iOS 7中,我看不到字体颜色的变化。下面是我的几段代码 CATextLayer *titleLayer = [CATextLayer layer]; titleLayer.frame = rect; titleLayer.alignmentMode = kCAAlignmentCenter; titleLayer.truncationMode =
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;
}
检查iOS 7或8是否成功工作
CTFontRef fontFace = CTFontCreateWithName((__bridge CFStringRef)(@"HelfaSemiBold"), 24.0, NULL);
NSString *title=@"Chirag Kalsariya";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:title ];
[attString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge id)fontFace range:NSMakeRange(0, [title length])];
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)[UIColor blueColor].CGColor range:NSMakeRange(0, 6)];
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)[UIColor blackColor].CGColor range:NSMakeRange(6, 10)];
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.frame = CGRectMake(0, 0, 300, 100);
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.truncationMode = kCATruncationEnd;
titleLayer.string = attString;
你好如果除了代码之外,还提供一些简短的解释,这通常会很有帮助。请看@Chirag Kalsariya,我不想贴标签。我想这样设置字符串:titleLayer.string=[self-attributedTitleAtIndex:idx];