2012-08-23 25 views
2

在xcode中添加UI按鈕時,我使用以下代碼來設置標題標籤的文本顏色,對齊方式和字體大小。字體大小被拾取,但不是文本顏色和對齊,爲什麼?謝謝。如何爲UIButton動態設置標籤顏色和對齊方式

toButton.titleLabel.textColor = [UIColor redColor]; 
toButton.titleLabel.textAlignment = UITextAlignmentRight; 
toButton.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE]; 
[toButton setTitle:fromButton.titleLabel.text forState:UIControlStateNormal]; 
+0

嘗試使用setter而不是propert IES。 [toButton.titleLabel setTextColor:[UIColor redColor]] –

+0

@ValeryPavlov Setter和屬性在運行時執行相同的代碼,沒有區別。 – Hyperbole

回答

7

你的顏色設置爲titleLabel,而不是這種情況,使用:

[toButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 

文本對齊的這條線將工作:

toButton.titleLabel.textAlignment = UITextAlignmentRight; 

您還可以使用:

toButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; 
+0

謝謝,它修復了標題顏色。怎麼樣的對齊,有沒有方法來設置按鈕標籤的對齊方式? – user1620383

+0

@ user1620383:我已經更新了答案:) –

+0

設置toButton.titleLabel.textAlignment不起作用。將嘗試contentHorizo​​ntalAlignment。 – user1620383

相關問題