iOS上如何讓按鈕(UIbutton)文本靠左對齊展示

來源:互聯網
上載者:User

標籤:pos   tracking   named   ack   解決   nts   效果   標籤   codec   

  1. // button.titleLabel.textAlignment = NSTextAlignmentLeft; 這句無效  
  2.       button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;  
  3.       button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);  

在做UI介面的時候一直困惑與如何讓button的文字靠左對齊展示,今天重新遇到。決定徹底解決問題

首先我們會想到以下的代碼

button.titleLabel.textAlignment = NSTextAlignmentLeft; 這行代碼是沒有效果的。這僅僅是讓標籤中的文本靠左對齊,但

並沒有改變標籤在button中的對齊。


所以。我們首先要使用

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 這行代碼,把按鈕的內容(控制項)

的對齊改動為水平靠左對齊,可是這們會緊緊靠著左邊,不好看,

所以我們還能夠改動屬性:

button.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);

這行代碼能夠讓button的內容(控制項)距離左邊8個像素。依據自己須要調整。也可為負數(--8)這樣就好看多了。希望對大家也有協助

2015.7.31改動

以下是button和文字上下對齊展示

int origin_Y;    origin_Y=64;    for (int i=0; i<4; i++) {        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, origin_Y,50, 50)];        btn.backgroundColor = [UIColor greenColor];        [btn setImage:[UIImage imageNamed:@"bbc"] forState:UIControlStateNormal];        [btn setTitle:@"測試" forState:UIControlStateNormal];        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        btn.titleLabel.backgroundColor = [UIColor blueColor];        //btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;        btn.imageEdgeInsets = UIEdgeInsetsMake(5,10,21,0);        btn.titleEdgeInsets = UIEdgeInsetsMake(30,-(btn.frame.size.width-10*2), 0, 0);        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:btn];        origin_Y+=(btn.frame.size.height+10);    }


如有問題歡迎不吝賜教!

iOS上如何讓按鈕(UIbutton)文本靠左對齊展示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.