Wyrównanie etykiet w systemie iOS 6 - uitextalignment przestarzały

Jest to bardzo proste i łatwe w obsłudze.

Nadal go używam i działa dobrze, ale daje ostrzeżenie. Jak mogę to naprawić?
label.textAlignment = UITextAlignmentCenter;
Dzięki.
Author: Nayan, 2012-08-12

10 answers

W iOS6 możesz użyć

label.textAlignment = NSTextAlignmentCenter;
Mam nadzieję, że to pomoże.
 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/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-31 21:15:52

Zmiana właściwości labelAlignment jest prawdopodobnie związana z wprowadzeniem przez Apple NSAttributedStrings do większej liczby kontrolek systemu iOS, a zatem koniecznością zmiany właściwości UIText... na nstext... properties.

Więc jeśli zaktualizowałeś do iOS6, jesteś w clover; po prostu przełącz się z UITextAlignmentCenter na NSTextAlignmentCenter i ciesz się nowymi, fantazyjnymi strunami.

Ale jeśli pracujesz nad złożonym projektem i wolałbyś, aby ziemia nie poruszała się tak bardzo pod twoimi stopami, możesz chcieć trzymać się starszej wersji na jakiś czas, i dostosować swój kod do wielu wersji, coś w tym stylu:

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

Powyższe podejście działa dla nowych metod ; dostajesz ostrzeżenia, ale wszystko działa dobrze. Ale kiedy kompilator widzi stałą, o której nie wie, zmienia kolor na czerwony i zatrzymuje się w swoich utworach. Nie ma sposobu, aby przemknąć NSTextAlignmentCenter przez to. (Cóż, może być sposób na dostosowanie zachowania kompilatora tutaj, ale wydaje się to niewskazane.)

Obejściem jest dodanie kilku preprocesor warunkowy definiuje. Jeśli umieścisz coś takiego w pliku h twojej klasy (lub może w importowanym pliku stałych -- który sam musi zawierać #import <UIKit/UIKit.h>, aby kiedykolwiek dowiedzieć się o NSText... stałe) ...

#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

...możesz to zrobić:

label.textAlignment = kLabelAlignmentCenter;

I to:

label.lineBreakMode = kLabelTruncationMiddle;

Itd.

Ponieważ te zmiany UIText/NSText mogą pojawiać się w przypadku wielu kontrolek, takie podejście jest całkiem przydatne.

(Zastrzeżenie: bycie członkiem wyżej wymienionych steady-earth lovers, testowałem to ze starą wersją, ale jeszcze nie z 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/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-27 22:56:21

NSTextAlignmentCenter może być używany zamiast UITextAlignmentCenter, a lista innych zamienników znajduje się poniżej:

#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/doraprojects.net/template/agent.layouts/content.php on line 54
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/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-17 10:35:40

Nie musisz tego robić. Xcode 4.5 skompiluje NSTextAlignmentCenter, itd. dobrze w 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/doraprojects.net/template/agent.layouts/content.php on line 54
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/doraprojects.net/template/agent.layouts/content.php on line 54
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/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-29 09:41:47

Miałem podobny problem i korzystałem z następujących: szczegóły.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/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-22 09:56:44

W iOS 6 lub większym

Użyj tej wartości:

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/doraprojects.net/template/agent.layouts/content.php on line 54
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/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-19 17:37:29