¿Se puede cambiar el tamaño de fuente de UILabel con una animación suave en el iPhone?


Quiero un UILabel para hincharse ligeramente cuando se selecciona como en algunas pantallas de menú del juego. Para obtener un cambio de tamaño suave, supongo que debería hacer algún cambio en las propiedades de la etiqueta en un bloque de animación.

Lo obvio es cambiar la etiqueta .letra.pointSize propiedad pero eso es readonly.

Escalando las etiquetas .transform property with CGAffineTransformationMakeScale () hace que el texto sea borroso.

¿Hay alguna otra manera de hacer esto?

Author: Hiren, 2010-01-20

5 answers

Establezca la fuente en la etiqueta UILabel para que tenga el tamaño que desee cuando se amplíe. Entonces reduce la escala. Cuando quieras que la etiqueta se hinche, vuelve a escalarla hasta su tamaño original.

    messageLabel.font = [UIFont boldSystemFontOfSize:45]; 
    messageLabel.transform = CGAffineTransformScale(messageLabel.transform, 0.25, 0.25); 
    [self.view addSubview:messageLabel]; 
    [UIView animateWithDuration:1.0 animations:^{
        messageLabel.transform = CGAffineTransformScale(messageLabel.transform, 4, 4);
    }];
 57
Author: Bart Whiteley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-21 16:23:37

ACTUALIZADO para Xcode 8 / Swift 3 / iOS 10 SDK.

He creado la extensión UILabel en Swift. Descomente las líneas comentadas si su etiqueta está alineada a la izquierda.

import UIKit

extension UILabel {
    func animateToFont(_ font: UIFont, withDuration duration: TimeInterval) {
        let oldFont = self.font
        self.font = font
        // let oldOrigin = frame.origin
        let labelScale = oldFont!.pointSize / font.pointSize
        let oldTransform = transform
        transform = transform.scaledBy(x: labelScale, y: labelScale)
        // let newOrigin = frame.origin
        // frame.origin = oldOrigin
        setNeedsUpdateConstraints()
        UIView.animate(withDuration: duration) {
        //    self.frame.origin = newOrigin
            self.transform = oldTransform
            self.layoutIfNeeded()
        }
    }
}

Objective-C versión:

@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
 11
Author: mixel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-03 19:45:54

Si desea animar de fontSize a fontSize, puede usar la clase CATextLayer

// create text layer 
let textLayer = CATextLayer()
textLayer.font = UIFont.systemFontOfSize(50)
textLayer.fontSize = 50
textLayer.string = "text"
textLayer.foregroundColor = UIColor.redColor().CGColor
textLayer.backgroundColor = UIColor.blackColor().CGColor
textLayer.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
view.layer.addSublayer(textLayer)

// animation
let animation = CABasicAnimation(keyPath: "fontSize")
animation.toValue = 10
animation.duration = 3
textLayer.layer.addAnimation(animation, forKey: nil)
 4
Author: ober,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-03 14:22:25

Acabo de hacer una solución para esto. Se basa en CGAffineTransformScale también, pero también tiene algunos otros trucos bajo la manga y maneja todos los casos fronterizos:

Compruébalo: https://github.com/ivankovacevic/ARLabel

 2
Author: Ivan Kovacevic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-16 12:00:02

No puede cambiar el tamaño de punto de la fuente, pero puede reemplazar la fuente de la etiqueta UILabel con un nuevo UIFont que use el nuevo tamaño de punto que desee.

label.font = [UIFont fontWithName:@"Arial-BoldMT" size:12.0];
// ... some time later
label.font = [UIFont fontWithName:@"Arial-BoldMT" size:14.0];

Probablemente también querrá modificar el marco de la etiqueta UILabel para acomodar el nuevo tamaño de punto, especialmente si está aumentando el tamaño de punto. De lo contrario, tu etiqueta será recortada.

 0
Author: Jay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-30 04:24:15