// 创建 圆角矩形 的按钮
02 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
03
04 // button 类型 有以下 6 种,
05 // typedef enum {
06 // UIButtonTypeCustom = 0, 自定义风格
07 // UIButtonTypeRoundedRect, 圆角矩形
08 // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
09 // UIButtonTypeInfoLight, 亮色感叹号
10 // UIButtonTypeInfoDark, 暗色感叹号
11 // UIButtonTypeContactAdd, 十字加号按钮
12 // } UIButtonType;
13
14 //给定button在 view 上的 位置
15 button1.frame = CGRectMake(20, 20, 280, 20);
16
17 //button 背景色
18 button1.backgroundColor = [UIColor clearColor];
19
20 //设置button填充 图片
21 //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal]; //设置button图片
UIImage* image = [UIImage imageNamed:@"2_0.jpg"];
//[button setImage:image forState:UIControlStateNormal];
//[button setBackgroundImage:image forState:UIControlStateNormal];
//拿到当前图片
image = button.currentImage;
image = button.currentBackgroundImage;
//按钮图片大小
//1、图片命名@2x.png
//2、[UIImage imageNamed:@"aa.png"] 图片会更具按钮的大小改变,而[UIImage imageWithContentsOfFile:imagePath] 真实显示图片大小
//NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"search-B@2x" ofType:@"png"];
22
23 //设置button 标题
24 [button1 setTitle:@"点击" forState:UIControlStateNormal];
25
//取得title
NSString* title = button.titleLabel.text;
title = button.currentTitle;
//设置title颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//取得title的颜色
UIColor* color = button.currentTitleColor;
26 /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
27 //以下是几种状态
28 // enum {
29 // UIControlStateNormal = 0, 常规状态显现
30 // UIControlStateHighlighted = 1 << 0, 高亮状态显现
31 // UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
32 // UIControlStateSelected = 1 << 2, 选中状态
33 // UIControlStateApplication = 0x00FF0000, 当应用程序标志时
34 // UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
35 // };
36
37 /*
38 * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
39 * 那么可以去掉这个功能
40 */
41 button1.adjustsImageWhenHighlighted = NO;
42 /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
43 button1.adjustsImageWhenDisabled = NO;
44 /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
45 button1.showsTouchWhenHighlighted = YES;
46
47 /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
48 按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
49 触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
50 也可以传入其他类的指针*/
51 [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
52
53
54 //显示控件
55 [self.view addSubview:button1];
/*
- (void)buttonClick:(UIButton*)button{
//父视图通过tag值获取子视图的指针对象
/*
子视图可以设置一个tag值,然后添加到父视图上,父视图就可以通过这个tag值拿到子视图的指针。
tag值也可以保存一些用户的信息。
*/
UILabel* label = (UILabel*)[self.window viewWithTag:100];
label.text = @"我被修改了";
}
*/
//文字对齐
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//文字内在距离
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//圆角 外边框颜色
[Button1.layer setMasksToBounds:YES];
[Button1.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
[Button1.layer setBorderWidth:1.0]; //边框宽度
//第一种颜色
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
[Button1.layer setBorderColor:colorref];//边框颜色
//第二种
Button1.layer.backgroundColor = (__bridge CGColorRef)([self colorFromHexRGB:TopSliderColor]);//边框颜色
//第三种
Button1.layer.borderColor=[UIColor grayColor].CGColor;//边框颜色
(QuartzCore.framework)
02 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
03
04 // button 类型 有以下 6 种,
05 // typedef enum {
06 // UIButtonTypeCustom = 0, 自定义风格
07 // UIButtonTypeRoundedRect, 圆角矩形
08 // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
09 // UIButtonTypeInfoLight, 亮色感叹号
10 // UIButtonTypeInfoDark, 暗色感叹号
11 // UIButtonTypeContactAdd, 十字加号按钮
12 // } UIButtonType;
13
14 //给定button在 view 上的 位置
15 button1.frame = CGRectMake(20, 20, 280, 20);
16
17 //button 背景色
18 button1.backgroundColor = [UIColor clearColor];
19
20 //设置button填充 图片
21 //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal]; //设置button图片
UIImage* image = [UIImage imageNamed:@"2_0.jpg"];
//[button setImage:image forState:UIControlStateNormal];
//[button setBackgroundImage:image forState:UIControlStateNormal];
//拿到当前图片
image = button.currentImage;
image = button.currentBackgroundImage;
//按钮图片大小
//1、图片命名@2x.png
//2、[UIImage imageNamed:@"aa.png"] 图片会更具按钮的大小改变,而[UIImage imageWithContentsOfFile:imagePath] 真实显示图片大小
//NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"search-B@2x" ofType:@"png"];
22
23 //设置button 标题
24 [button1 setTitle:@"点击" forState:UIControlStateNormal];
25
//取得title
NSString* title = button.titleLabel.text;
title = button.currentTitle;
//设置title颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//取得title的颜色
UIColor* color = button.currentTitleColor;
26 /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
27 //以下是几种状态
28 // enum {
29 // UIControlStateNormal = 0, 常规状态显现
30 // UIControlStateHighlighted = 1 << 0, 高亮状态显现
31 // UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
32 // UIControlStateSelected = 1 << 2, 选中状态
33 // UIControlStateApplication = 0x00FF0000, 当应用程序标志时
34 // UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
35 // };
36
37 /*
38 * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
39 * 那么可以去掉这个功能
40 */
41 button1.adjustsImageWhenHighlighted = NO;
42 /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
43 button1.adjustsImageWhenDisabled = NO;
44 /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
45 button1.showsTouchWhenHighlighted = YES;
46
47 /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
48 按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
49 触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
50 也可以传入其他类的指针*/
51 [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
52
53
54 //显示控件
55 [self.view addSubview:button1];
/*
- (void)buttonClick:(UIButton*)button{
//父视图通过tag值获取子视图的指针对象
/*
子视图可以设置一个tag值,然后添加到父视图上,父视图就可以通过这个tag值拿到子视图的指针。
tag值也可以保存一些用户的信息。
*/
UILabel* label = (UILabel*)[self.window viewWithTag:100];
label.text = @"我被修改了";
}
*/
//文字对齐
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//文字内在距离
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//圆角 外边框颜色
[Button1.layer setMasksToBounds:YES];
[Button1.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
[Button1.layer setBorderWidth:1.0]; //边框宽度
//第一种颜色
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
[Button1.layer setBorderColor:colorref];//边框颜色
//第二种
Button1.layer.backgroundColor = (__bridge CGColorRef)([self colorFromHexRGB:TopSliderColor]);//边框颜色
//第三种
Button1.layer.borderColor=[UIColor grayColor].CGColor;//边框颜色
(QuartzCore.framework)