二、 UIButton的使用总结

 

1.     初始化

l   最普通的初始化方法

UIButton *btn = [[UIButton alloc] initWithFrame:rect];

l   快速初始化

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

type参数用来指定按钮的类型,一共有6种选择:

UIButtonTypeCustom:无类型,按钮的内容需要自定义

UIButtonTypeRoundedRect:圆矩形边框 

UIButtonTypeDetailDisclosure:

UIButtonTypeInfoLight:

UIButtonTypeInfoDark:

UIButtonTypeContactAdd:

 

2.     设置文字

l   设置按钮在默认状态下显示的文字(第2个state参数用来指定按钮的状态)

[btn setTitle:@"登录" forState:UIControlStateNormal];

l   设置按钮在长按状态下显示的文字

[btn setTitle:@"登录2" forState:UIControlStateHighlighted];

 

3.     设置文字颜色

l   设置按钮在默认状态下的文字颜色为红色

[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

修改第2个参数就可以设置其他状态下的文字颜色

 

4.     设置字体

l   先初始化字体对象,再设置字体

// 使用系统自带的字体

UIFont *font = [UIFont systemFontOfSize:16];

// 设置字体

btn.titleLabel.font = font;

l   也可以使用其他字体

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:16];

 

5.     设置背景颜色

l   设置背景颜色为橙色

[btn setBackgroundColor:[UIColor orangeColor]];

 

6.     设置背景图片

l   先加载图片,再设置图片

// 加载图片

UIImage *image = [UIImage imageNamed:@"lufy.png"];

// 设置默认状态下的背景图片

[btn setBackgroundImage:image forState:UIControlStateNormal];

修改第2个参数就可以设置其他状态下的背景图片

 

7.     添加监听器

[btn addTarget:self action:@selector(click:)

forControlEvents:UIControlEventTouchUpInside];

u  最后一个参数用来指定事件类型,这里传入的是单击事件

u  按钮被单击后,就会调用self的click:方法,并且将按钮作为方法的第一个参数传入

- (void) click:(UIButton *)btn

{

}

posted @ 2014-07-06 09:07  ゴルツの爱萍纳閣下  阅读(148)  评论(0编辑  收藏  举报