iOS UIButton的基本使用

來源:互聯網
上載者:User

標籤:ios

UIButton的基本用法,平時用的也就是這麼多,其他遇到在加

//執行個體化2種方法

    UIButton *btn1 = [[UIButton alloc] init];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    //座標和大小

    btn.frame = CGRectMake(100, 100, 100, 100);

    //背景顏色

    btn.backgroundColor = [UIColor redColor];

    //設定字型

    [btn setTitle:@"UIButton" forState:UIControlStateNormal];

    //設定字型顏色

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

    //設定層的弧度

    btn.layer.cornerRadius = 50.0f;

    //多餘的層是否隱藏

    btn.layer.masksToBounds = YES;

    //設定層的寬度

    btn.layer.borderWidth = 1.0f;

    //設定層的顏色

    btn.layer.borderColor = [[UIColor yellowColor]CGColor];

    //標記

    btn.titleLabel.tag = 100;

    //設定文字的大小

    btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];

    //文字加粗

    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];

    //設定圖片

    [btn setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

    //設定背景圖片

    [btn setBackgroundImage:[UIImage imageNamed:@"1.png"]  forState:UIControlStateNormal];

    //設定透明度

    [btn setAlpha:0.5f];

    //是否選擇

    [btn setSelected:YES];

    //是否隱藏

    [btn setHidden:NO];

    //是否高亮

    [btn setHighlighted:YES];

    //是否可以觸摸

    [btn setEnabled:YES];

    //設定是否互動事

    [btn setUserInteractionEnabled:YES];

    //設定文字的位移

    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    //設定圖片的位移

    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    //垂直

    [btn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

    //水平

    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

    //文字置中

    btn.titleLabel.textAlignment = NSTextAlignmentCenter;

    //設定中心座標

    [btn setCenter:CGPointMake(100, 100)];

    //設定文字陰影顏色

    [btn setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];

    //文字發光

    [btn setShowsTouchWhenHighlighted:YES];

    //點擊事件

    [btn addTarget:self action:@selector(goToView:) forControlEvents:UIControlEventTouchUpInside];

    //加入視圖中

    [self.view addSubview:btn];


本文出自 “11204872” 部落格,請務必保留此出處http://11214872.blog.51cto.com/11204872/1744047

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.