0

How to change the color of the UILabel text gradually like the following link? Can any one suggest me some code?

Hasib Samad
  • 1,081
  • 1
  • 20
  • 39
Fazil
  • 1,390
  • 2
  • 13
  • 26
  • 1
    [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Pfitz Dec 15 '12 at 10:03
  • i tried with this http://stackoverflow.com/questions/3315148/how-to-animate-text-color-of-an-uilabel – Fazil Dec 15 '12 at 10:13

4 Answers4

2

You can use formatted text.

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

iOS < 6

Second you need to subclass UILabel and print this string inside the drawRect method. You need to create a some type of loop that changes the color according to the speech speed.

iOS 6

You can use the attributedTextproperty (no need to subclass)

  • (void)drawTextInRect:(CGRect)rect

or reuse code:

https://github.com/mattt/TTTAttributedLabel

Roger
  • 7,535
  • 5
  • 41
  • 63
0

Use NSAtributedString in UILabel from iOS 6.0. For lesser version below iOS 6.0 use TTTAttributedLabel which supports NSAtributedString

Change attributed string according to your requirement by setting it again in UILabel

EDIT add colored text as u want for example in loop

For 1st second in Label : I am good boy.

For 2nd second in Label : I am good boy.

For 3rd second in Label : I am good boy.

For 4th second in Label : I am good boy.

For 5th second in Label : I am good boy.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

here is one of my sample code. using block method of TTTAttributedLabel class it may help you .

        [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;
    }];
jogi47
  • 105
  • 2
  • 9
0

The app that you linked [http://www.youtube.com/watch?v=_vOYvaNhSHw] , probably is maded using cocos2d.
In cocos2d, you can change text color easily also with animation.

Here an example:
http://www.cocos2d-iphone.org/forum/topic/5903

Here cosos2d sdk, i suggest to try, because it's very powerful:
http://www.cocos2d-iphone.org/

enjoy.

elp
  • 8,021
  • 7
  • 61
  • 120