UILabel tamaño de fuente?


Parece que no puedo modificar el tamaño de fuente de una UILabel con el siguiente código:

itemTitle.font = [UIFont systemFontOfSize:25];

A medida que aumento el número 25 a algo mayor, parece que solo agrega un margen superior a la etiqueta, lo que consecuentemente empuja el texto hacia abajo tanto, de modo que el texto se corta en la parte inferior o se desborda completamente.

Tengo otra etiqueta UILabel en otro lugar con systemFontOfSize 25, y es mucho más pequeña que el texto itemTitle. ¿Qué está pasando? ¿No se supone que 25 es un valor absoluto?

Estoy tan confundido sobre cómo cambiar programáticamente el tamaño de fuente de uilabels.

Author: Krunal, 2012-05-07

10 answers

Compruebe que sus etiquetas no están configuradas para cambiar el tamaño automáticamente. En IB, se llama "Autoshrink" y está justo al lado de la configuración de fuente. Programáticamente, se llama adjustsFontSizeToFitWidth.

 56
Author: Rob Napier,
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
2012-05-07 01:00:33

He modificado la etiqueta UILabel mediante el siguiente código:

label.font=[label.font fontWithSize:25];

Pruebe esto y vea si está funcionando en su caso o no???

 167
Author: xtreme,
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
2012-05-07 19:54:07
[label setFont:[UIFont systemFontOfSize:9]];

Esto funciona para mí.

 32
Author: John Paul Manoza,
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-03-18 09:59:38

* * Puede establecer el tamaño de la fuente por estas propiedades * *

timedisplayLabel= [[UILabel alloc]initWithFrame:CGRectMake(70, 194, 180, 60)];

[timedisplayLabel setTextAlignment:NSTextAlignmentLeft];

[timedisplayLabel setBackgroundColor:[UIColor clearColor]];

[timedisplayLabel setAdjustsFontSizeToFitWidth:YES];

[timedisplayLabel setTextColor:[UIColor blackColor]];

[timedisplayLabel setUserInteractionEnabled:NO];

[timedisplayLabel setFont:[UIFont fontWithName:@"digital-7" size:60]];

timedisplayLabel.layer.shadowColor =[[UIColor whiteColor ]CGColor ];

timedisplayLabel.layer.shadowOffset=(CGSizeMake(0, 0));

timedisplayLabel.layer.shadowOpacity=1;

timedisplayLabel.layer.shadowRadius=3.0;

timedisplayLabel.layer.masksToBounds=NO;

timedisplayLabel.shadowColor=[UIColor darkGrayColor];

timedisplayLabel.shadowOffset=CGSizeMake(0, 2);
 11
Author: kapil,
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
2015-05-25 12:04:44

Para Swift 3.1 y Swift 4, si sólo desea cambiar el tamaño de fuente para una etiqueta :

let myLabel : UILabel = ...
myLabel.font = myLabel.font.withSize(25)
 7
Author: Kevin ABRIOUX,
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
2018-01-31 06:45:18

Método muy simple, pero eficaz para ajustar el tamaño del texto de la etiqueta de forma progresiva: -

label.font=[UIFont fontWithName:@"Chalkduster" size:36];

:-)

 6
Author: Madhur Sodhi,
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-12-10 11:50:03

Esto funcionó para mí:

sequencerPlayLabel.font = [UIFont fontWithName:kTypeFont size:kTypeFontSize];

- rich

 1
Author: user2887097,
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
2014-04-17 14:48:39

Esto funcionó para mí en

Swift 3

label.font = label.font.fontWithSize(40.0)

Swift 4

label.font = label.font.withSize(40.0)
 1
Author: Krunal,
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-10-27 10:14:31

Las respuestas anteriores ayudaron mucho.

Aquí está la versión Swift.

@IBOutlet weak var priceLabel: UILabel!

*.... lines of code later*

self.priceLabel.font = self.priceLabel.font.fontWithSize(22)
 0
Author: Christopher Wade Cantley,
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
2015-05-12 13:40:12

Intenta cambiar la altura y el ancho del tamaño del marco de la etiqueta para que el texto no se corte.

 [label setframe:CGRect(x,y,widht,height)];
 -1
Author: vishiphone,
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
2012-05-07 19:54:00