UILabel Alinear texto al centro


¿Cómo alineo texto en UILabel?

Author: Bartłomiej Semańczyk, 2011-04-20

9 answers

Desde iOS 6 y posteriores UITextAlignment está obsoleto. use NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Versión Swift de iOS 6 y posteriores

myLabel.textAlignment = .center
 546
Author: Aravindhan,
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-04-30 11:58:35

Aquí hay un código de ejemplo que muestra cómo alinear texto usando UILabel:

label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.textAlignment = NSTextAlignmentCenter;

Puedes leer más al respecto aquí UILabel

 81
Author: Vishal Shah,
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-07-28 19:03:44

Para centrar el texto en una etiqueta UILabel en Swift (que está dirigida para iOS 7+) puede hacer:

myUILabel.textAlignment = .Center

O

myUILabel.textAlignment = NSTextAlignment.Center
 11
Author: AdamT,
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-08-24 04:34:43

N.B.: De acuerdo con la referencia de clase UILabel , a partir de iOS 6 este enfoque ahora está obsoleto.

Simplemente use la propiedad textAlignment para ver la alineación requerida usando uno de los valores UITextAlignment. (UITextAlignmentLeft, UITextAlignmentCenter o UITextAlignmentRight.)

E. g.: [myUILabel setTextAlignment:UITextAlignmentCenter];

Vea la referencia de la clase UILabel para más información.

 8
Author: John Parker,
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-11-01 09:31:48

Use yourLabel.textAlignment = NSTextAlignmentCenter; para iOS >= 6.0 y yourLabel.textAlignment = UITextAlignmentCenter; para iOS

 6
Author: Jayprakash Dubey,
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-02-12 23:25:33

Para Swift 3 es:

label.textAlignment = .center
 5
Author: Michał Kwiecień,
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-12-19 09:27:38

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

 4
Author: Rinju Jain,
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-04-19 07:25:01
Label.textAlignment = NSTextAlignmentCenter;
 3
Author: Mubin Shaikh,
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-07-18 10:26:10

En xamarin ios supongamos que el nombre de su etiqueta es title y luego haga lo siguiente

title.TextAlignment = UITextAlignment.Center;
 2
Author: Dilip Jangid,
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-12-16 07:11:25