Wygląda na UITextAlignmentCenterto, że jest przestarzałe w iOS 6.
Nadal go używam i działa dobrze, ale daje ostrzeżenie.
Jak mogę to naprawić?
label.textAlignment = UITextAlignmentCenter;
Dzięki.
Wygląda na UITextAlignmentCenterto, że jest przestarzałe w iOS 6.
Nadal go używam i działa dobrze, ale daje ostrzeżenie.
Jak mogę to naprawić?
label.textAlignment = UITextAlignmentCenter;
Dzięki.
Odpowiedzi:
W iOS6 możesz użyć
label.textAlignment = NSTextAlignmentCenter;
Mam nadzieję że to pomoże.
Zmiana labelAlignmentwłaściwości jest prawdopodobnie związana z wprowadzeniem przez Apple NSAttributStrings do większej liczby elementów sterujących iOS, a zatem wymaga zmiany właściwości UIText… na NSText….
Jeśli więc uaktualniłeś system do iOS6, jesteś koniczyną; wystarczy przełączyć od UITextAlignmentCenterdo NSTextAlignmentCenteri cieszyć się fantazyjne nowe struny.
Ale jeśli pracujesz nad złożonym projektem i wolisz, aby Ziemia nie poruszała się tak bardzo pod stopami, możesz przez pewien czas trzymać się starszej wersji i dostosować swój kod do wielu wersji, coś takiego:
// This won't compile:
if ([label respondsToSelector:@selector(attributedText:)])
label.textAlignment = UITextAlignmentCenter;
else
label.textAlignment = NSTextAlignmentCenter;
Powyższe podejście działa w przypadku nowych metod ; otrzymujesz ostrzeżenia, ale wszystko działa dobrze. Ale gdy kompilator zobaczy stałą , o której nie wie, zmienia kolor na czerwony i zatrzymuje się. Nie ma sposobu, aby się wymknąć NSTextAlignmentCenter. (Cóż, może istnieć sposób na dostosowanie zachowania kompilatora tutaj, ale wydaje się to niewskazane).
Obejściem tego problemu jest dodanie niektórych warunkowych definicji preprocesora. Jeśli umieścisz coś takiego w pliku h swojej klasy (lub być może w importowanym pliku stałych - który sam musi zawierać #import <UIKit/UIKit.h>, aby kiedykolwiek wiedzieć o stałych NSText ...)…
#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;
Itp.
Ponieważ zmiany UIText / NSText prawdopodobnie pojawią się w przypadku wielu kontrolek, takie podejście jest całkiem przydatne.
(Zastrzeżenie: Będąc członkiem wyżej wymienionych miłośników stałej ziemi, przetestowałem to na starej wersji, ale jeszcze nie na iOS6.)
NSTextAlignmentCentermoże być użyty zamiast, UITextAlignmentCentera 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
#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];
Nie musisz tego robić. Xcode 4.5 skompiluje NSTextAlignmentCenteritp. W porządku w iOS 5.
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];
labelName.textAlignment=NSTextAlignmentLeft;
Miałem podobny problem i użyłem następującego: detailsLabel.textAlignment = NSTextAlignmentCenter;
w iOS 6 lub nowszym
użyj tej wartości:
self.lbl_age.textAlignment=NSTextAlignmentCenter;
Swift 3:
label.textAlignment = NSTextAlignment.center
NSTextAlignment.