简体   繁体   中英

How can I animate the size of UILabel or UITextField text?

I have a UITextfield in a UIView that contains some default text. I want to animate the scaling of this textField (along with the parent UIView) %300, and later back down to 100%.

How do I do this? It seems everything I've found involves a hacky image capture and a CGAffineTransformMakeScale.

I've spent way to much time searching SO for this, either it's difficult and/or costly to do or utterly trival to implement. I wouldn't be surprised if it's the latter. :)

You don't have to capture the image from the view, you can simply use scale transform on your label using the following code

[UIView beginAnimations:nil context:nil];
label.transform = CGAffineTransformMakeScale(3.0, 3.0);
[UIView commitAnimations];

But the only problem with this approach is that the text might not appear crisp.

If you want the scaling of your UILabel to be crisp than I suggest you try core text framework. This will definitely help you in achieving your goal.

A tutorial for the same can be found here .

Reference: Animations-Explained

ios/documentation/CoreAnimation_guide

CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position.x";
animation.values = @[ @0, @10, @-10, @10, @0 ];
animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
animation.duration = 0.4;

animation.additive = YES;

[form.layer addAnimation:animation forKey:@"shake"];

OR

iOS - Animating text size change in UILabel or UITextView?

This might helps you :)

In Swift 4.1

Using UIView.animate(withDuration:) does not work for FontSize. and using CGAffineTransform is hard to manage, and has to be applied to the UILabel or UITextField , but as it is said in the accepted response, the result is not crisp.

This is the solution I am using.

var fontSize: CGFloat = 12.0
Timer.scheduledTimer(withTimeInterval: 0.02, repeats: true) { (timer) in
    self.font = UIFont(name: "Roboto-Regular", size: fontSize)
    fontSize += 0.5 // Or 0.333 to allow for 3X resolution screens.
    if fontSize > 17.0 {
        timer.invalidate()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM