How to Animate Changing a Uilabel's Textalignment

Is there a way to animate changing a UILabel's textAlignment?

Set the UILabel frame size to exactly contain the text and center the UILabel in your view.

self.monthLabel.text = @"February";
[self.monthLabel sizeToFit];
self.monthLabel.center = parentView.center; // You may need to adjust the y position

Then set the alignment which should not affect the layout since there will be no extra space.

self.monthLabel.textAlignment = NSTextAlignmentLeft;

Next, animate the UILabel frame size so it slides over where you want it.

[UIView animateWithDuration:0.5
animations:^{
CGRect frame = self.monthLabel.frame;
frame.origin.x = 10;
self.monthLabel.frame = frame;
} completion:nil];

Animate text change in UILabel

Here is the code to make this work.

[UIView beginAnimations:@"animateText" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1.0f];
[self.lbl setAlpha:0];
[self.lbl setText:@"New Text";
[self.lbl setAlpha:1];
[UIView commitAnimations];

UILabel animating improperly

You could add an UIView and then add UILabel as its subview:

 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(40, 20, 100, 50)];
[view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:view];

UILabel *label = [[UILabel alloc] init];
[label setText:@"Label Name"];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:17.0]];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label sizeToFit];
[label setNumberOfLines:0];
[label setFrame:CGRectOffset(label.frame, (view.frame.size.width - label.frame.size.width)/2, (view.frame.size.height - label.frame.size.height)/2)];
[view addSubview:label];

[UIView animateWithDuration:3 animations:^{
[view setFrame:CGRectMake(40, 20, 200, 300)];
[label setFrame:CGRectMake((view.frame.size.width - label.frame.size.width)/2, (view.frame.size.height - label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)];
} completion:^(BOOL finish){

}];

Animate Text Change of UILabel

Use aUIView Extension

Animate in place using a single label:

- Leverage upon the built-in CALayer animations, and a UIView extension

- In the Storyboard, place the UILabel inside a UIView with Clip Subviews flag.

- pushTransition is applicable to most UIView

1. Extension

// Usage: insert view.pushTransition right before changing content
extension UIView {
func pushTransition(_ duration:CFTimeInterval) {
let animation:CATransition = CATransition()
animation.timingFunction = CAMediaTimingFunction(name:
kCAMediaTimingFunctionEaseInEaseOut)
animation.type = kCATransitionPush
animation.subtype = kCATransitionFromTop
animation.duration = duration
layer.add(animation, forKey: kCATransitionPush)
}
}

2. Invocation

if let aLabel = label {
aLabel.pushTransition(0.4) // Invoke before changing content
aLabel.text = "\(count)"
count += 1
}

3. Example

Animation with a kCAMediaTimingFunctionEaseInEaseOut curve, a 0.4 second duration, and a kCATransitionFromTop direction .

CALayer Animation


(†) Clipping is an important step for desired effect.

Swift 2 and earlier, see Steve Baughman's comment:
self.layer.addAnimation(animation, forKey: kCATransitionPush)

► Find this solution on GitHub and additional details on Swift Recipes.

Animating Frame of UILabel smoothly

Hooray for answering a two-year dead question, but I found the answer. Either in Interface Builder or in code, change the contentMode property of the label. Yours seems to be set on scaleToFill; try left or right.

How to animate UILabel text size (and color)

As I mentioned, applying a CGAffineTransform to a UILabel to scale it is frustrating for design-oriented developers, for two reasons.

  1. The transform doesn't account for tracking and font variants. San Francisco (Apple's system font) uses two distinct variants of its font depending on the text size.

iOS automatically applies the most appropriate variant based on the point size and the user's accessibility settings. Adjust tracking—the spacing between letters—appropriately.

SF Pro Text is applied to text 19 points or smaller, while SF Pro Display is applied to text 20 points or larger. Each variant has different "tracking" values — the spacing between letters — for each point size (see: the many tracking values under Font Usage and Tracking).

Unfortunately, CGAffineTransform doesn't set a new pointSize, which would otherwise force a redraw of the view. Applying a transform just scales the rasterized bitmap of the UILabel, which means that fonts and tracking values aren't adaptive. So, if we're scaling our label by 2.0 from 10pt to 20pt, our resulting UILabel at 20pt will still be using SF Pro Text with 12pt tracking, instead of SF Pro Display with 19pt tracking. You can see below that the red label is transformed and hence does not adjust its font.


  1. Transforming a view or layer leads to blurriness. As previously alluded to, Core Graphics doesn't re-render the view, but rather transforms the view's 2D map of pixels. The red label below is clearly blurry.

The Solution

Our best bet is to use CATextLayer instead of UILabel, which Apple says is:

A layer that provides simple text layout and rendering of plain or attributed strings.

The CATextLayer docs list var string: Any?, var font: CFTypeRef?, var fontSize: CGFloat, var foregroundColor: CGColor? and more as properties, which most of the time is all we need. Perfect!

All we need to do is add a CAPropertyAnimation — like CABasicAnimation — to the CATextLayer to animate its properties, like so:

// Create the CATextLayer
let textLayer = CATextLayer()
textLayer.string = "yourText"
textLayer.font = UIFont.systemFont(ofSize: startFontSize)
textLayer.fontSize = startFontSize
textLayer.foregroundColor = UIColor.black.cgColor
textLayer.contentsScale = UIScreen.main.scale
textLayer.frame = view.bounds
view.layer.addSublayer(textLayer)

// Animation
let duration: TimeInterval = 10
textLayer.fontSize = endFontSize
let fontSizeAnimation = CABasicAnimation(keyPath: "fontSize")
fontSizeAnimation.fromValue = startFontSize
fontSizeAnimation.toValue = endFontSize
fontSizeAnimation.duration = duration
textLayer.add(fontSizeAnimation, forKey: nil)

and voila! The black text below is our properly scaled CATextLayer. Sharp and with the correct font variant.

Alt Text

h/t to Yinan for the code: https://stackoverflow.com/a/42047777/4848310. Yinan also discusses how to use CATextLayer with Auto Layout constraint-based animations.


Bonus! This also works for animating text color! Just use the foregroundColor property.

How to change the uilabel text when animate an uiimageview

You can store description of all images in an NSArray with same order as you add images in array. For example, array[0]'s description in imageDescription[0] and likewise.

Then when you start image animation, along with that call this method

index = 0;
[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(showDesc) userInfo:nil repeats:YES];
[imgview_banner startAnimating ];

Then define showDesc() method as shown below :

- (void)showDesc {
self.label.text = imageDescription[index];
index ++;
if (index == array.count) {
index = 0;
}
}

Declare a variable index as global.



Related Topics



Leave a reply



Submit