Alineación de etiquetas en iOS 6 - UITextAlignment obsoleto


Parece que UITextAlignmentCenter está obsoleto en iOS 6.

Todavía lo uso y funciona bien, pero da una advertencia.

¿Cómo puedo arreglar esto?

label.textAlignment = UITextAlignmentCenter;

Gracias.

Author: Nayan, 2012-08-12

10 answers

En iOS6 puede usar

label.textAlignment = NSTextAlignmentCenter;

Espero que esto ayude.

 387
Author: majorl3oat,
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-01-31 21:15:52

El cambio de propiedad labelAlignment probablemente esté relacionado con la introducción de NSAttributedStrings de Apple en más de los controles de iOS, y por lo tanto necesita cambiar las propiedades UIText properties a las propiedades NSText properties.

Así que si has actualizado a iOS6, estás en clover; simplemente cambia de UITextAlignmentCenter a NSTextAlignmentCenter y disfruta de las nuevas cadenas de fantasía.

Pero si está trabajando con un proyecto complejo y prefiere que la tierra no se mueva tanto bajo sus pies, es posible que desee seguir con una versión anterior por un tiempo, y adaptar su código para múltiples versiones, algo como esto:

// This won't compile:
if ([label respondsToSelector:@selector(attributedText:)]) 
    label.textAlignment = UITextAlignmentCenter;
else 
    label.textAlignment = NSTextAlignmentCenter;

El enfoque anterior funciona para nuevos métodos ; obtienes advertencias pero todo funciona bien. Pero cuando el compilador ve una constante que no conoce, se vuelve roja y se detiene en su camino. No hay manera de colarse NSTextAlignmentCenter más allá de él. (Bueno, podría haber una manera de personalizar el comportamiento del compilador aquí, pero parece desaconsejable.)

La solución es agregar algunos preprocesador condicional define. Si pones algo como esto en el archivo h de tu clase (o tal vez en un archivo de constantes importado which que debe incluir #import <UIKit/UIKit.h> para saber alguna vez sobre el NSText... constantes)...

#ifdef NSTextAlignmentCenter // iOS6 and later
#   define kLabelAlignmentCenter    NSTextAlignmentCenter
#   define kLabelAlignmentLeft      NSTextAlignmentLeft
#   define kLabelAlignmentRight     NSTextAlignmentRight
#   define kLabelTruncationTail     NSLineBreakByTruncatingTail 
#   define kLabelTruncationMiddle   NSLineBreakByTruncatingMiddle
#else // older versions
#   define kLabelAlignmentCenter    UITextAlignmentCenter
#   define kLabelAlignmentLeft      UITextAlignmentLeft
#   define kLabelAlignmentRight     UITextAlignmentRight
#   define kLabelTruncationTail     UILineBreakModeTailTruncation
#   define kLabelTruncationMiddle   UILineBreakModeMiddleTruncation
#endif

Can puedes hacer esto:

label.textAlignment = kLabelAlignmentCenter;

Y esto:

label.lineBreakMode = kLabelTruncationMiddle;

Etc.

Dado que es probable que estos cambios de UIText/NSText aparezcan para múltiples controles, este enfoque es bastante útil.

(Advertencia: Ser miembro de la mencionada amantes de la tierra firme, he probado esto con una versión antigua, pero aún no con iOS6.)

 44
Author: Wienke,
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-09-27 22:56:21

NSTextAlignmentCenter puede ser utilizado en lugar de UITextAlignmentCenter y una lista de otros reemplazos está abajo:

#ifdef __IPHONE_6_0 // iOS6 and later
#   define UITextAlignmentCenter    NSTextAlignmentCenter
#   define UITextAlignmentLeft      NSTextAlignmentLeft
#   define UITextAlignmentRight     NSTextAlignmentRight
#   define UILineBreakModeTailTruncation     NSLineBreakByTruncatingTail
#   define UILineBreakModeMiddleTruncation   NSLineBreakByTruncatingMiddle
#endif
 19
Author: nhisyam,
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-02-08 00:03:57
#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
label.text = @"There is no spoon";
label.textAlignment = ALIGN_CENTER;
[self addSubview:label];
 15
Author: neoneye,
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-17 10:35:40

No tienes que hacer ninguna de estas cosas. Xcode 4.5 compilará NSTextAlignmentCenter, etc. bien en iOS 5.

 13
Author: Aaron Brager,
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-05-03 08:46:15
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 40)];
[label1 setText:@"Your String"];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 sizeToFit];

//For Center Alignment
[label1 setTextAlignment:NSTextAlignmentCenter];

//For Right Alignment
[label1 setTextAlignment:NSTextAlignmentRight];

//For Left Alignment
[label1 setTextAlignment:NSTextAlignmentLeft];

// Add the label into the view

[self.view addSubview:label1];
 2
Author: Gaurav Gilani,
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-05-21 13:41:58
labelName.textAlignment=NSTextAlignmentLeft;
 1
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-12-29 09:41:47

Tuve un problema similar y usé lo siguiente: detailsLabel.textAlignment = NSTextAlignmentCenter;

 0
Author: BrainyMonkey,
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-07-22 09:56:44

En iOS 6 o superior

Utilice este valor:

self.lbl_age.textAlignment=NSTextAlignmentCenter;

 0
Author: Hiren Dhamecha,
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-03-30 10:56:37

Swift 3:

label.textAlignment = NSTextAlignment.center
 0
Author: Jerry Krinock,
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-10-19 17:37:29