標籤: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的基本使用