更改动画的UILabel颜色

时间:2012-12-15 10:00:27

标签: iphone ios uilabel

如何逐渐更改UILabel文本的颜色,如下所示link? 任何人都可以建议我一些代码吗?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在iOS 6.0的NSAtributedString中使用UILabel。对于iOS 6.0以下的较低版本,请使用支持NSAtributedString

TTTAttributedLabel

根据您attributed string requirement setting again UILabel改变colored text

编辑根据需要添加{{1}},例如在循环中

标签中的第一秒:我是个好孩子。

标签中的第二秒:是个好男孩。

标签中的第3秒:我上午好孩子。

在Label中获得第4名:我是男孩。

标签中的第5秒:我很好男孩

答案 2 :(得分: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;
    }];

答案 3 :(得分:0)

您关联的应用[http://www.youtube.com/watch?v=_vOYvaNhSHw]可能会使用cocos2d进行评分。
cocos2d中,您还可以使用动画轻松更改文字颜色。

这里有一个例子:
http://www.cocos2d-iphone.org/forum/topic/5903

这里是cosos2d sdk,我建议试试,因为它非常强大:
http://www.cocos2d-iphone.org/

享受。