2011-04-19 28 views

Respuesta

539

De iOS 6 y más tarde UITextAlignment está en desuso. utilizar NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter; 

Swift Versión de iOS 6 y más tarde

myLabel.textAlignment = NSTextAlignment.center 
+8

se puede simplificar la versión rápida :) myLabel.textAlignment = .Center – aryaxt

+1

.center ← minúsculas – lufasz

8

N.B .: Según el UILabel class reference, a partir de iOS 6, este enfoque está en desuso.

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

ej .: [myUILabel setTextAlignment:UITextAlignmentCenter];

Véase el UILabel Class Reference para más información.

81

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

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

Puede leer más sobre esto aquí UILabel

+27

'UITextAlignment' está en desuso desde iOS 5. Use' NSTextAlignment' en su lugar. – Philip007

+0

False, UITextAligment está en desuso. En UIStringDrawing.h (UIKit) se puede encontrar este código: // 'Deprecated: utilizar NSTextAlignment enumeración en UIKit/NSText.h typedef NS_ENUM (NSInteger, UITextAlignment) { UITextAlignmentLeft = 0, UITextAlignmentCenter, UITextAlignmentRight, // podía add justified in future } NS_DEPRECATED_IOS (2_0,6_0); ' – aramusss

4

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

6

Uso yourLabel.textAlignment = NSTextAlignmentCenter; para iOS> = 6,0 y yourLabel.textAlignment = UITextAlignmentCenter; para iOS < 6.0.

3
Label.textAlignment = NSTextAlignmentCenter; 
11

Para centrar el texto en un UILabel en Swift (que se apunta para iOS 7+) que puede hacer:

myUILabel.textAlignment = .Center 

O

myUILabel.textAlignment = NSTextAlignment.Center 
2

En ios Xamarin suponga que su etiqueta nombre es el título entonces haga lo siguiente

title.TextAlignment = UITextAlignment.Center; 
+2

La pregunta no es sobre Xamarin. Y 'UITextAlignment' está [en desuso en iOS 6] (https://developer.apple.com/reference/uikit/uitextalignment?language=objc)! – FelixSFD

5

Para Swift 3 es:

label.textAlignment = .center 
Cuestiones relacionadas