Rock
Rock

Reputation: 1510

Font color does not change for ios7 CATextLayer

I am using attributed string for my text its working fine for iOS 6 and 8 but for iOS 7 I cannot see font color changes. Below is few blocks of code of mine.

  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;
}

Upvotes: 1

Views: 163

Answers (1)

Chirag Kalsariya
Chirag Kalsariya

Reputation: 256

Check on both iOS 7 or 8 Successfully Work...

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;

Upvotes: 1

Related Questions