2012-12-15 63 views
0

如何逐漸更改UILabel文本的顏色,如下所示link? 任何人可以建議我一些代碼?更改UILabel顏色動畫

+1

[你有什麼試過?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Pfitz

+0

我試過這個http://stackoverflow.com/questions/3315148/how-to-animate-text-of-an-uilabel – Fazil

回答

2

您可以使用格式化文本。

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello World"]; 
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(1,5)]; 

的iOS 6 <

其次,你需要繼承的UILabel和打印drawRect方法這裏面的字符串。您需要創建一些根據語音速度改變顏色的循環。

的iOS 6

可以使用attributedTextproperty(無需子類)

  • (無效)drawTextInRect:(的CGRect)RECT

或重用代碼:

https://github.com/mattt/TTTAttributedLabel

0

使用NSAtributedString來自iOS 6.0的UILabel。根據您的requirement通過settingagainUILabel

編輯添加colored text爲u環路

對於第1條第二想例如下面的iOS 6.0中使用TTTAttributedLabel支持NSAtributedString

變化attributed string較小版本在標籤:我是好孩子。

對於第二個標籤:是好孩子。

標籤中的第3秒:I am好孩子。

標籤中的第4秒:我是男孩。

標籤中的第5秒:我很好男孩

0

這裏是我的示例代碼之一。使用TTTAttributedLabel類的塊方法可能對您有所幫助。

 [cell.lblAtt setText:strAtt afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { 

     UIFont *italicSystemFont = [UIFont boldSystemFontOfSize:12]; 
     CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL); 

     NSUInteger lenght = [[tempObj objectForKey:@"username"] length]; 
     NSUInteger lenght2 = [[NSString stringWithFormat:@"%d",[tempArr count]] length]; 

     [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(0,lenght)]; 
     [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(0,lenght)]; 

     [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(lenght+11,lenght2)]; 
     [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(lenght+11,lenght2)]; 

     return mutableAttributedString; 
    }]; 
+0

這裏的細胞是什麼? –

+1

這不重要,可能是'UITableViewCell' ...'lblAtt'就是你的標籤。 – elp

+0

@prince cell是tableview的。 lblAtt是它的標籤。 –