UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
UIButton类型:
UIButtonTypeCustom //自定义类型
添加图片:
灰色背景颜色:
UIButtonTypeRoundedRect //圆角类型
UIButtonTypeDetailDisclosure //细节展示按钮
UIButtonTypeInfoLight //浅色背景的信息按钮
UIButtonTypeInfoDark //暗色背景的信息按钮
UIButtonTypeContactAdd // 添加按钮
创建UIButton
1. UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake (x, y, Width, Height)];
2. UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
设置UIButton标题
[button setTitle:title forState:UIControlStateNormal];
设置UIButton标题颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
设置UIButton背景图片
[button setBackgroundImage:newImage forState:UIControlStateNormal];
设置UIButton背景颜色
button.backgroundColor = [UIColor clearColor];
转自:http://blog.sina.com.cn/s/blog_a250468201010h9f.html
设置按钮上的字体的大小
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
设置UIButton上字体的对齐方式
[zanbut setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[zanbut setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[zanbut setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
UIButton *zanbut = [[UIButtonalloc]initWithFrame:CGRectMake(210,25, 50,20)];
[zanbut setTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];
[zanbut setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];//BUTTON上字体对齐方式
[zanbut setBackgroundImage:[UIImageimageNamed:@"赞图标.png"]forState:UIControlStateNormal];
[bgv addSubview:zanbut];
[zanbut release];
或者:
有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
转自:http://my.oschina.net/clownfish/blog/56812
02 | UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
15 | button1.frame = CGRectMake(20, 20, 280, 20); |
18 | button1.backgroundColor = [UIColor clearColor]; |
24 | [button1 setTitle:@ "点击" forState:UIControlStateNormal]; |
41 | button1.adjustsImageWhenHighlighted = NO; |
43 | button1.adjustsImageWhenDisabled = NO; |
45 | button1.showsTouchWhenHighlighted = YES; |
51 | [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside]; |
55 | [self.view addSubview:button1]; |