2011-04-19 54 views

Trả lời

539

Từ iOS 6 và sau UITextAlignment bị phản đối. sử dụng NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter; 

Swift Version từ iOS 6 và sau đó

myLabel.textAlignment = NSTextAlignment.center 
+8

phiên bản nhanh có thể được đơn giản hóa :) myLabel.textAlignment = .Center – aryaxt

+1

.center ← chữ thường – lufasz

8

N.B .: Theo UILabel class reference, khi iOS 6 phương pháp này hiện không được chấp nhận.

Chỉ cần sử dụng thuộc tính textAlignment để xem căn chỉnh theo yêu cầu bằng cách sử dụng một trong các giá trị UITextAlignment. (UITextAlignmentLeft, UITextAlignmentCenter hoặc UITextAlignmentRight.)

ví dụ .: [myUILabel setTextAlignment:UITextAlignmentCenter];

Xem UILabel Class Reference để biết thêm thông tin.

81

Dưới đây là một số mẫu mã cho thấy làm thế nào để sắp xếp văn bản sử dụng UILabel:

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

Bạn có thể đọc thêm về nó ở đây UILabel

+27

'UITextAlignment' không được dùng nữa vì iOS 5. Sử dụng' NSTextAlignment' thay thế. – Philip007

+0

Sai, UITextAligment không được dùng nữa. Trong UIStringDrawing.h (UIKit), bạn có thể tìm thấy mã này: '// Deprecated: sử dụng NSTextAlignment enum trong UIKit/NSText.h typedef NS_ENUM (NSInteger, UITextAlignment) { UITextAlignmentLeft = 0, UITextAlignmentCenter, UITextAlignmentRight, // thể thêm hợp lý trong tương lai } NS_DEPRECATED_IOS (2_0,6_0); ' – aramusss

4

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

6

Sử dụng yourLabel.textAlignment = NSTextAlignmentCenter; dành cho iOS> = 6.0 và yourLabel.textAlignment = UITextAlignmentCenter; dành cho iOS < 6.0.

3
Label.textAlignment = NSTextAlignmentCenter; 
11

Để tập trung văn bản trong một UILabel trong Swift (được nhắm mục tiêu cho iOS 7 trở lên), bạn có thể làm:

myUILabel.textAlignment = .Center 

Hoặc

myUILabel.textAlignment = NSTextAlignment.Center 
2

Trong ios Xamarin giả nhãn của bạn tên là tiêu đề, sau đó thực hiện như sau

title.TextAlignment = UITextAlignment.Center; 
+2

Câu hỏi không phải là về Xamarin. Và 'UITextAlignment' là [không được chấp nhận trong iOS 6] (https://developer.apple.com/reference/uikit/uitextalignment?language=objc)! – FelixSFD

5

Đối với Swift 3:

label.textAlignment = .center 
Các vấn đề liên quan