1. 程式人生 > >UIButton 內容對齊方式

UIButton 內容對齊方式

有些時候我們想讓設定UIButton的title居左對齊

btn.textLabel.textAlignment = UITextAlignmentLeft

是沒有作用的,我們需要設定

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是問題又出來,此時文字會緊貼到做邊框,我們可以設定

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距離做邊框保持10個畫素的距離。

=======================================================

設定UIButton上字型的顏色設定UIButton上字型的顏色,不是用:

[btn.titleLabel setTextColor:[UIColorblackColor]];

btn.titleLabel.textColor=[UIColor redColor];

而是用:

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

另外,設定按鈕的圖片時儘量使用setBoundgroupImage,不要使用setImage