UIButton经常用到的属性
参考:http://www.bubuko.com/infodetail-656634.html
- (void)buildUI{
//初始化
CGRect btnControlRect=CGRectMake(200,200, 100,30);
UIButton *btnControl = [UIButtonbuttonWithType:UIButtonTypeCustom];
btnControl.frame = btnControlRect;
//圆角
btnControl.layer.cornerRadius =5;
btnControl.layer.masksToBounds =YES;
// 设置按钮的setBackgroundImage
[btnControlsetBackgroundImage:[UIImageimageNamed:@"desc_nomal.png"]forState:UIControlStateNormal];
[btnControlsetBackgroundImage:[UIImageimageNamed:@"desc_nomal.png"]forState:UIControlStateHighlighted];
[btnControlsetBackgroundImage:[UIImageimageNamed:@"desc_nomal.png"]forState:UIControlStateSelected];
[btnControlsetBackgroundColor:[UIColorclearColor]];
//设置按钮的text及color
[btnControlsetTitle:@"预定"forState:UIControlStateNormal];
[btnControlsetTitle:@"取消"forState:UIControlStateSelected];
[btnControlsetTitleColor:[UIColorcolorWithRed:50/255.0green:50/255.0blue:50/255.0alpha:1.0]forState:UIControlStateNormal];
[btnControlsetTitleColor:[UIColorcolorWithRed:255/255.0green:255/255.0blue:255/255.0alpha:1.0]forState:UIControlStateSelected];
// 行数,字号
[btnControl.titleLabelsetNumberOfLines:2];
[btnControl.titleLabelsetFont:[UIFontsystemFontOfSize:14]];
// 设置按钮文本对齐方式
btnControl.titleLabel.textAlignment =NSTextAlignmentCenter;
// 设置按钮图片和文字的相对位置 (top,left,bottom,right)
UIImage *image = [UIImageimageNamed:@""];
btnControl.titleEdgeInsets =UIEdgeInsetsMake(0, -(image.size.width),0, 0);//文字在右
btnControl.imageEdgeInsets =UIEdgeInsetsMake(0,0, 0, btnControl.frame.size.width-image.size.width);//图片在左
//设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上左下右的距离。
btnControl.contentEdgeInsets =UIEdgeInsetsMake(0,0, 0,0);
[btnControladdTarget:selfaction:@selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnControl];
}
//点击事件
- (void)ClickControlAction:(UIButton *)button{
if (button.selected!=YES) {
button.selected=YES;
}else{
button.selected=NO;
}
}