2010-12-16 69 views
7

這裏是我的代碼:的UILabel中心內容

UILabel *myLabel; 
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 480)]; 
myLabel.lineBreakMode = UILineBreakModeWordWrap; 
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines 
myLabel.text = @"Please select at least one image category!"; 
myLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size: 24.0]; 
myLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); 
myLabel.backgroundColor = [UIColor clearColor]; 
[self.view addSubview:myLabel]; 
[myLabel release]; 

我在想,如果有可能,因爲它是alined左側居中行文本: alt text

回答

26
myLabel.textAlignment=UITextAlignmentCenter; 
+9

NSTextAlignmentCenter for ios6 – fionbio 2013-02-04 19:48:54

12

UITextAlignmentCenter是正確的方法來居中文本,但現在折舊。 - see this question and answer.

使用NSTextAlignmentCenter代替:

label.textAlignment = NSTextAlignmentCenter; 

這是在評論中提到,只是把清楚像我這樣的新手。

1

使用此:

myLabel.textAlignment = NSTextAlignmentCenter; 

UITextAlignmentCenter從iOS 6中棄用!