UPDATED for Xcode 8 / Swift 3 / iOS 10 SDK.
I created the UILabel extension in Swift. Uncomment commented out the lines if your label is left justified.
import UIKit extension UILabel { func animateToFont(_ font: UIFont, withDuration duration: TimeInterval) { let oldFont = self.font self.font = font
Objective-C version:
@interface UILabel(FontAnimation) - (void) animateToFont:(UIFont*)font withDuration:(NSTimeInterval) duration; @end @implementation UILabel(FontAnimation) - (void) animateToFont:(UIFont*)font withDuration:(NSTimeInterval) duration { UIFont * oldFont = self.font; self.font = font; // CGPoint oldOrigin = self.frame.origin; CGFloat labelScale = oldFont.pointSize / font.pointSize; CGAffineTransform oldTransform = self.transform; self.transform = CGAffineTransformScale(self.transform, labelScale, labelScale); // CGPoint newOrigin = self.frame.origin; // self.frame = CGRectMake(oldOrigin.x, oldOrigin.y, self.frame.size.width, self.frame.size.height); [self setNeedsUpdateConstraints]; [UIView animateWithDuration:duration animations: ^{ // self.frame = CGRectMake(newOrigin.x, newOrigin.y, self.frame.size.width, self.frame.size.height); self.transform = oldTransform; [self layoutIfNeeded]; }]; } @end
mixel Sep 29 '15 at 20:44 2015-09-29 20:44
source share