UIButton上圖片和文字的位置調整,uibutton圖片

來源:互聯網
上載者:User

UIButton片和文字的位置調整,uibutton圖片

      UIButton 上預設是圖片在左文字在右,而大多數情況這樣預設的的顯示形式都不能滿足我們的需求,接下來我就這個問題分享一下我的心得。

      預設情況下,不設定的效果,都是置中實現

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(50, 50, 150, 100);

    button.backgroundColor = [UIColor yellowColor];

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

    [button setTitle:@"title" forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"tab5"] forState:UIControlStateNormal];

    [self.view addSubview:button];

*********************************************************

UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);

上面的四個數值是基於原位置而改變的例如:

    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 60, 0, 0)];

title 的CGFloat right改變的60 是基於原位置的titleLabel的右邊框向左平移60也就是到右邊框的距離。

image也是一樣,距離原來imageView的左邊框向右平移了60。

*********************************************************

    [button setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 30)];

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 30, 0)];

上下移動的原理同左右;

 

***********************************************

* 注意:這4個數值的位移都是基於原來的位置進行移動的例如第  *

* 一個數就是基於原來上邊框的位置向下移動,正數向下移動負數 *

* 向上移動;左右同理;                                                    *

***********************************************

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.