IOS学习笔记(三)之UIButton
2016-03-29 23:39
435 查看
龟速前行中,UIButton主要花时间学习了代码方式创建使用。
UIButton创建方式分为两种:intWithFrame、buttonWithType
UIButtonType
typedef enum {
UIButtonTypeCustom = 0, // 自定义,无风格
UIButtonTypeRoundedRect, / 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
UIButton添加事件:
[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]
例如:
UIButton *landBtn=[self
createButtonFrame:CGRectMake(10,
190,
self.view.frame.size.width-20,
37)
backImageName:nil
title:@"登录"
titleColor:[UIColor
whiteColor]
font:[UIFont
systemFontOfSize:19]
target:self
action:@selector(landClick)];
landBtn.backgroundColor=[UIColor
colorWithRed:248/255.0f
green:144/255.0f
blue:34/255.0f
alpha:1];
landBtn.layer.cornerRadius=5.0f;
-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor
*)color font:(UIFont *)font target:(id)target action:(SEL)action
{
UIButton *btn=[UIButton
buttonWithType:UIButtonTypeCustom];
btn.frame=frame; //设置button的在屏幕中的位置,大小
if (imageName)
{
[btn setBackgroundImage:[UIImage
imageNamed:imageName] forState:UIControlStateNormal];
}
if (font)
{
btn.titleLabel.font=font;
}
if (title)
{
[btn setTitle:title
forState:UIControlStateNormal];
}
if (color)
{
[btn setTitleColor:color
forState:UIControlStateNormal];
}
if (target&&action)
{
[btn addTarget:target
action:action forControlEvents:UIControlEventTouchUpInside];
}
return btn;
}
UIButton创建方式分为两种:intWithFrame、buttonWithType
UIButtonType
typedef enum {
UIButtonTypeCustom = 0, // 自定义,无风格
UIButtonTypeRoundedRect, / 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;
UIButton常用属性
//设置对应状态的标题内容default is nil. title is assumed to be single line- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
UIButton添加事件:
[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]
例如:
UIButton *landBtn=[self
createButtonFrame:CGRectMake(10,
190,
self.view.frame.size.width-20,
37)
backImageName:nil
title:@"登录"
titleColor:[UIColor
whiteColor]
font:[UIFont
systemFontOfSize:19]
target:self
action:@selector(landClick)];
landBtn.backgroundColor=[UIColor
colorWithRed:248/255.0f
green:144/255.0f
blue:34/255.0f
alpha:1];
landBtn.layer.cornerRadius=5.0f;
-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor
*)color font:(UIFont *)font target:(id)target action:(SEL)action
{
UIButton *btn=[UIButton
buttonWithType:UIButtonTypeCustom];
btn.frame=frame; //设置button的在屏幕中的位置,大小
if (imageName)
{
[btn setBackgroundImage:[UIImage
imageNamed:imageName] forState:UIControlStateNormal];
}
if (font)
{
btn.titleLabel.font=font;
}
if (title)
{
[btn setTitle:title
forState:UIControlStateNormal];
}
if (color)
{
[btn setTitleColor:color
forState:UIControlStateNormal];
}
if (target&&action)
{
[btn addTarget:target
action:action forControlEvents:UIControlEventTouchUpInside];
}
return btn;
}
相关文章推荐
- Fluent Nhibernate and Stored Procedures
- 用requests爬取百度搜索数据
- Quicksum -SilverN
- Android Volley完全解析(三),定制自己的Request
- [leetcode 300] Longest Increasing Subsequence
- iOS之UILabel自适应大小
- CEGUI0.8.4支持中文
- ERROR 1366 (HY000): Incorrect string value: '\xE7\x9A\x84' for column 'name' at row 1
- 另种获取key和value
- lesson 4 Building a Dynamic UI with Fragments
- easyui布局加TAB
- AutoLayout——何为intrinsic content size
- Android UI模板设计---TopBar(标题栏)
- request.getParameterMap()
- UITableviewcell的详尽
- 生成全球唯一标识GUID
- leetcode:回溯——permutation-sequence,
- 300.LeetCode Longest Increasing Subsequence(medium)[动态规划]
- 问题easyui树形成了死循环
- 0329 复利计算器5.0 Juint单元测试 组员 254列志华 253韩麒麟