我尝试了以下代码:
[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];
}
但我希望下面的图片类型改变颜色。
答案 0 :(得分:2)
UILabel现在支持属性字符串。所以你可以做类似于以下的事情
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;
}
但是归因于字符串有其局限性。 如果您希望实现真正精美的文本,请拔出袜子并检查Text Programming Guide和CoreText Framework。你可以找到很多tutorials for Core Text
答案 1 :(得分:0)
您必须使用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++;
}