2

I have a label and text inside it. I want my text to move across the label's width like a digital information board. How to do that in iOS? I tried with that code (which I got from here: http://www.youtube.com/watch?v=EFoNEjPwTXM ), but it doesn't work:

In the .m file:

-(void)time: (NSTimer *) theTimer
{
    currentSong.center = CGPointMake(currentSong.center.x - 3.5, currentSong.center.y);
    if (currentSong.center.x < - (currentSong.bounds.size.width/2))
    {
        currentSong.center = CGPointMake (320 + (currentSong.bounds.size.width/2), currentSong.center.y);
    }
}

In viewDidLoad:

timer = [NSTimer timerWithTimeInterval:0.09 target:self selector:@selector(time:) userInfo:nil repeats:YES];

In the .h file:

    IBOutlet UILabel *currentSong;
    IBOutlet NSTimer *timer;    

-(void)time: (NSTimer *) theTimer;

@end
Undo
  • 25,519
  • 37
  • 106
  • 129
scourGINHO
  • 699
  • 2
  • 12
  • 31
  • 2
    That's called a "Marquee": http://stackoverflow.com/questions/11255988/continuous-scrolling-of-uilabel-like-marquee – trojanfoe Mar 14 '13 at 13:04

4 Answers4

0

Try this may help

-(void)viewDidLoad {

    [super viewDidLoad];

    [self marqueeMessage:@"test"];
    // Do any additional setup after loading the view, typically from a nib.
}



- (void)marqueeMessage:(NSString *)messageString {
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))];
    //label.tag=nextIndex;
    label.text = messageString;
     label.backgroundColor=[UIColor clearColor];
    [self.view addSubview:label];
    [UIView beginAnimations:@"test" context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationDidStopSelector:@selector(marqueeMessage:)];
    [UIView setAnimationDelegate:self];

    label.frame = CGRectMake(360,50,90,21);
    [UIView commitAnimations];
}
rdurand
  • 7,342
  • 3
  • 39
  • 72
08442
  • 521
  • 2
  • 13
  • Thanks, but my idea was to move only the text, not the whole label. And if it is possible to be in the opposite direction. From right to left, I mean. – scourGINHO Mar 14 '13 at 13:29
0

Try this...it may meet u r requirement

 int x;
float size;
@synthesize label;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    label=[[UILabel alloc]init ];
           //WithFrame:CGRectMake(300, 400, 400, 30)];
    label.text=@"www.stackoverflow.com";
    size=[self getLabelWidth:label];
    label.frame=CGRectMake(300, 400, size, 30);

    label.backgroundColor=[UIColor clearColor];
    x=300;

    [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(clickme) userInfo:nil repeats:YES];

}

-(void)clickme
{
    x--;
    label.frame=CGRectMake(x, 400, size, 30);
    if( x==-size)
    {
        x=300;
    }
    [self.view addSubview:label];
}

-(float)getLabelWidth:(UILabel*)label1
{

    CGSize maximumSize = CGSizeMake(500,30);
    CGSize StringSize = [label1.text sizeWithFont:label1.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation];
    NSLog(@"width is %f",StringSize.width);
    return StringSize.width;
}
08442
  • 521
  • 2
  • 13
0

This is a bit of a hack. What it does is it simply adds spaces periodically before the text already in the label so the text looks like it's moving while actually the label isn't. Here's the code:

//Interface (.h)
@property (nonatomic, strong) IBOutlet UILabel *label; //Remember to connect this to    
                                                       //a label in Storyboards

//Implementation (.m)
-(void)moveText {
    [self.label setText:[NSString stringWithFormat:@" %@", self.label.text]];
}

- (void)viewDidLoad {
    [self.label setText:@"A very long and boring string"];

    //You can change the time interval to change the speed of the animation
    [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(moveText) userInfo:nil repeats:YES];
}
pasawaya
  • 11,515
  • 7
  • 53
  • 92
0
- (void)sendNotification:(NSString*)txt{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:messageView cache:YES];
    [lblTime setText:txt];
    [lblTime setTextAlignment:UITextAlignmentLeft];
    lblTime.frame = CGRectMake(260, 0, 258, 19);
    [UIView commitAnimations];
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
klewjl
  • 1