如何在UILabel中按顺序显示字符串中的字符?

时间:2013-01-03 11:56:15

标签: iphone ios uilabel

在UILabel中,您是否可以显示一个字符串,以便逐个字符填写?例如,如果标签文本是“IPHONE”,我想显示“I”,然后显示“IP”,然后显示“IPH”,一直显示“IPHONE”。

我怎么能以这种方式显示标签?

2 个答案:

答案 0 :(得分:7)

所以你要显示一个字符串的第一个字母,然后在一段时间后显示它的第二个字母等。解决方案:使用定时调用方法的计时器;该方法将适当地设置文本:

NSString *theTextToDisplay = @"Hello world!";

[NSTimer scheduledTimerWithTimeInterval:0.33
                                 target:self
                               selector:@selector(timerShoot:)
                               userInfo:nil
                                repeats:YES];

- (void)timerShoot:(NSTimer *)tmr
{
    static int pos = 1;
    theUILabel.text = [theTextToDisplay substringToIndex:pos];
    if (++pos > theTextToDisplay.length) {
        [tmr invalidate];
    }
}

答案 1 :(得分:1)

这只是一个示例,只是一个用动画显示文本的逻辑..

首先使用波纹管框架添加UILable,然后将文本设置为贝娄......

 yourLable.frame = CGRectMake(0, 112, 0, yourLable.frame.size.height); 
 yourLable.text = @"IPHONE";

之后,当您想要显示UILable

时,请调用此方法
-(IBAction)btnLableShow_Clicked:(id)sender
{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];

    yourLable.frame = CGRectMake(0, 112, 220, yourLable.frame.size.height);  // set width with your requirement
    [UIView commitAnimations];
}