有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
=======================================================
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:selfaction:@selector(buttonDown:)forControlEvents:UIControlEventTouchDown];//设置事件有冒号的方法带参数
[btn addTarget:selfaction:@selector(btnUp)forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"这就去拿" forState:UIControlStateNormal];//设置标题
[btn setTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
[btn setTitleColor:[UIColorwhiteColor]forState:UIControlEventTouchDown];
btn.frame = CGRectMake(80.0,210.0,160.0, 40.0);//大小位置
[btn setBackgroundImage:[UIImageimageNamed:@"btn1.jpg"]forState:UIControlStateNormal];//设置背景图片
[btn setBackgroundImage:[UIImageimageNamed:@"btn2.jpg"]forState:UIControlEventTouchDown];//设置按下去时的图片
#import <QuartzCore/QuartzCore.h>
//设置登录按钮边框
loginBtn.tag =2;
CALayer * downButtonLayer = [loginBtnlayer];
[downButtonLayersetMasksToBounds:YES];
[downButtonLayersetCornerRadius:10.0];
[downButtonLayersetBorderWidth:1.0];
[downButtonLayer setBorderColor:[[UIColorcolorWithRed:7/255.0green:98/255.0blue:182/255.0alpha:1]CGColor]];
[self.viewinsertSubview:loginBtnatIndex:loginBtn.tag];
待续。。。
-(void)buttonDown:(id)sender{
//UIButton* btn = (UIButton*)sender;
NSLog(@"Button pushed down");
}
-(void)btnUp{
NSLog(@"btn is up");
}
其他:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
UIButtonTypeCustom //自定义类型
添加图片: 灰色背景颜色:
UIButtonTypeRoundedRect //圆角类型
UIButtonTypeDetailDisclosure //细节展示按钮
UIButtonTypeInfoLight //浅色背景的信息按钮
UIButtonTypeInfoDark //暗色背景的信息按钮
UIButtonTypeContactAdd // 添加按钮