IOS Marquee Text

by / Monday, 30 September 2013 / Published in Ipad, Iphone

MarqueeLabel is a UILabel subclass adds a scrolling marquee effect when the text of the label outgrows the available width. The label scrolling direction and speed/rate can be specified as well. All standard UILabel properties (where it makes sense) are available in MarqueeLabel and it behaves just like a UILabel.

Here we will show how to animate label text throughout the UILabel in our project.To do so,first of all we need to add QuartzCore framework to our project from Linked Frameworks section.Now in UIStoryboard we need to take a UILabel and a UIButton.We will animate the UILabel text on clicking the Button.

Now in .h file of our controller we need to add following:

@interface ViewController : UIViewController{

UIView *messageView;

CGSize messageSize;

}

We also need to take a string variable to maintain the start stop activity of label text animation.Now in .m file of our controller we need to add following method:

- (void)animateLabeltext:(UILabel *)newView{

NSString * lbltest=newView.text;

newView.center=CGPointMake(ReqLbl.frame.size.width+newView.frame.size.width/2,newView.center.y);

[UIView animateWithDuration:lbltest.length/8

delay:0.0

options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction

animations:^(void) {

newView.center=CGPointMake(-newView.frame.size.width/2,newView.center.y);

}

completion:^(BOOL finished) {

if(finished) {

[self animateLabeltext:newView];

}

}

];

}

Now in button on click we need to do the following manipulation:

if([self.animationstatus isEqualToString:@"no"]){

self.animationstatus=@"yes";

[self.animatebutton setTitle:@"Stop Animating" forState:UIControlStateNormal];

[self animateLabeltext:ReqLbl];

}

else{

self.animationstatus=@"no";

self.ReqLbl.frame=CGRectMake(8.0, 20.0, 303.0, 80.0);

[self.animatebutton setTitle:@"Start Animating" forState:UIControlStateNormal];

[self.ReqLbl.layer removeAllAnimations];

}

TOP