0

I tried below code:

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTimerMethod:) userInfo:nil repeats:NO];

-(void)myTimerMethod:(NSTimer*)timer
{

    label.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.22 alpha:1.0];

}

but i want like below image type to change color.

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
RameshIos
  • 301
  • 1
  • 4
  • 13
  • Your question is unclear. What is happening with the code you posted, and what exactly do you want? – rdelmar Jul 03 '14 at 15:59

2 Answers2

2

UILabel now supports attributed string. So you can do something similar to following

int lengthOfTextToColor = 0;
NSMutableAttributedString *textString = [[NSMutableAttributedString alloc] initWithString:@"I think you are making a karaoke app"];

-(void)colorChangeTimer:(NSTimer *)timer {
    lengthOfTextToColor ++;
    NSRange range = NSMakeRange(0, lengthOfTextToColor);
    NSDictionary *colorAttribute = @{NSForegroundColorAttributeName:[UIColor blueColor]};
    [textString setAttributes: colorAttribute range:range];
    self.label.attributedText = textString;
}

But attributed string has its limitations. If you are looking to implement really fancy text, pull up your socks and check Text Programming Guide and CoreText Framework. You can find many tutorials for Core Text

Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
  • Thank You its working for me...but add to this also if (lengthOfTextToColor==[textString length]) { [timer invalidate]; return; } – RameshIos Jul 04 '14 at 10:23
0

You have to use NSMutableAttributedString:

_string = [[NSMutableAttributedString alloc] initWithString:@"your string"];
_index = 0;
[NSTimer scheduledTimerWithInterval:1.0 target:self selector:@selector(yourSelector) userInfo:nil repeats:NO];

- (void)yourSelector {
    [_string setAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0 green:0 blue:0 alpha:1]} range:NSMakeRange(_index, 1)];
    [label setAttributedText:_string];
    _index++;
}
Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107