2015-09-14 26 views
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; 
} 

回答

1

檢查兩個iOS的7或8成功地工作...

CTFontRef fontFace = CTFontCreateWithName((__bridge CFStringRef)(@"HelfaSemiBold"), 24.0, NULL); 
NSString *[email protected]"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; 
+0

嗨!除了你的代碼之外,你還提供了一些簡短的解釋。見https://stackoverflow.com/help/how-to-answer –

+0

@Chirag Kalsariya我不想拿標籤。我想這樣設置字符串:titleLayer.string = [self attributedTitleAtIndex:idx]; – Rock