Esto realmente no es demasiado difícil de hacer. Necesita crear dos IBActions en su clase, en Touch Down y on Touch Up. Crea un IBOutlet para tu botón también. A continuación, vaya a Interface Builder y conecte el botón a IBOutlet y conecte la acción de "Touch Down" a "onTouchDown" y las acciones de "Touch Up Inside" y "Touch Up Outside" a "onTouchUp".
onTouchDown es donde configurará la fuente resaltada. onTouchUp es donde reiniciarás tu botón.
Su archivo de cabecera va a terminar buscando algo como esto:
@interface TestViewController : UIViewController {
UIButton *testButton;
}
@property (nonatomic, retain) IBOutlet UIButton *testButton;
- (IBAction)onTouchDown;
- (IBAction)onTouchUp;
Ahora, dentro de la función "onTouchDown" esto es lo que vamos a hacer:
1) Almacenar las dimensiones actuales de la botón
2) Cambiar el tamaño de la fuente
3) Diga el botón de ajuste de tamaño automático (esta es la clave)
4) Centro en el botón en función del tamaño de edad
Usted debe tener una función que termina buscando algo como esto:
- (IBAction)onTouchDown:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:36];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
Nota, el tamaño de fuente es 36.
En su función de retoque, que se verá algo como esto:
- (IBAction)onTouchUp:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:15];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
El tamaño de fuente debe ser cualquiera que sea su fuente por defecto es . En este caso, lo hice 15.
Esto debería obtener un botón que cambia de tamaño desde el centro en lugar de simplemente cambiar el tamaño.
Ahora, el código no pretende ser perfecto. Es solo una IDEA general de lo que estás haciendo aquí. Creo que este es un gran candidato para la reutilización de algunos códigos al combinarlos en una función y pasar el tamaño que desea que tenga el texto.Yo no tenía ganas de hacer lo mismo;)
Thnx @warrenm ur código es mucho más simple y fácil :) –
'NSString * fontName = button.titleLabel.font.fontName; button.titleLabel.font = [UIFont fontWithName: fontName size: 19.0f]; ' Esto conserva la fuente actual. – Michiel