1.建立⼀一个 button :buttonWithType:UIButton*button=[UIButtonide
buttonWithType:UIButtonTypeRoundedRect]; //加方法建立,不须要 release函数
//建立⼀一个什么样的 button 用 buttonWithType:字体
typedef enum {
UIButtonTypeCustom = 0, //自定义(常加载图片) nospa
button type
UIButtonTypeRoundedRect, //圆角按钮UIButtonTypeDetailDisclosure, //尖叫号按钮UIButtonTypeInfoLight, 信息按钮(浅)UIButtonTypeInfoDark, 信息按钮(深)UIButtonTypeContactAdd, 加号按钮线程
} UIButtonType;
2.设置坐标和大小
button.frame = CGRectMake(10, 30, 300, 30);指针
3.// 设置按钮上的文字
// 在按钮默认状态(正常状态)下显示:圆角按钮code
[btn1 setTitle:@"圆角按钮" forState:UIControlStateNormal];orm
UIControlStateNormal = 0, 正常状态UIControlStateHighlighted = 1 << 0, 高亮状态(点击按钮时)UIControlStateDisabled 按钮无效未激活时(enabled==NO)对象
// 在按钮点住状态(高亮状态下)下显示:按钮被点击事件
[btn1 setTitle:@"按钮被点击"forState:UIControlStateHighlighted];3.//更改按钮文字大小
btn.titleLabel.font = [UIFont systemFontOfSize:25] ;
4.// 设置点住时按钮变化颜色(高亮状态)
[btn1 setTintColor:[UIColor redColor]];
// 设置背景颜色后能够看出其实仍是⼀一个矩形的View
[btn1 setBackgroundColor:[UIColor orangeColor]];
5. // 设置按钮的点击事件
/*
target:执行哪一个对象中的方法
action:执行的方法controlEvents:触发的方式
*/
[btn1 addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
UIControlEventTouchDown
鼠标左键按下触发
鼠标按下,在button内抬起 鼠标按下,在button外抬起
UIControlEventTouchUpInside
触发
UIControlEventTouchUpOutside
触发
6.//生成⼀一个定时器,时间每隔0.02秒调用⼀一次updateTimer 函数,函数声
明在self-本类内部 repeats:Yes表明重复调用
第二个参数能够是任意类型的对象指针
[NSTimer scheduledTimerWithTimeInterval:0.02target:self selector:@selector(updateTimer) userInfo:nilrepeats:YES] ;
NSTimer * timer; if(!timer)
timer = [NSTimer scheduledTimerWithTimeInterval:0.02target:self selector:@selector(setLabel) userInfo:nilrepeats:YES];
//启动⼀一个定时器,第⼀一个参数表示延迟时间,是浮点数,单位是秒.最后 ⼀一个参数表示是否重复,添NO表示这个事情只作⼀一次。
//启动新的线程0.02秒钟后,让self 调用 setLabel
//定时器不须要启动,从建立时就自动启动了,不须要释放,停下来时,
自动释放。
else{
[timer invalidate];//终止定时器,定时器会释放
timer = nil; }
7.tag 标签:能够区分是哪⼀一个控件; view viewWithTag:<#(NSInteger)#>];
.
[self.view viewWithTag:11];
view viewWithTag:<#(NSInteger)#>];
UILabel
//实际上 label 就是⼀一个能够显示文字的视图控件;1.//initWithFrame设置标签的坐标和大小
UILabel * label1 = [[UILabelalloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
2.//给⼀一个视图添加⼀一个子视图;[self.view addSubview:label1];
3.//给Label添加内容(设置)label2.text =@"hi,我是⼀一个标签. ";
4.//设置字体颜色 设置标签内容的颜色label2.textColor = [UIColor whiteColor];
5.//设置标签背景颜色label.backgroundColor = [UIColor
orangeColor];//clearColor透明色
6.//设置字体的大小
label.font = [UIFont systemFontOfSize:40]; label.font = [UIFont boldSystemFontOfSize:20];//加粗
label.font = [UIFont italicSystemFontOfSize:20];//倾斜7.//自适应宽度 默认是NO
label.adjustsFontSizeToFitWidth = YES;8.//设置对齐方式;
//高版本以NS开头设置对齐方式label1.textAlignment =
UITextAlignmentCenter;//UITextAlignmentRight右对齐UITextAlignmentCenter居中显示默认左对齐
9.//多行显示
label.numberOfLines= 5;//0最大行数(默认是1)//label 的内容超过标签的宽度,默认省略的后面的内容
10//换行与省略
当内容超过 label 的宽度时:
label.lineBreakMode = UILineBreakModeWordWrap;//以
单词为单位换行(最后⼀一行显示不完以单词截断剩下的内容不显示也 不会省略(没有...))、、高版本 NS 开头
typedef enum {/////////如下三种会换行和截断不会有省略
UILineBreakModeWordWrap = 0, UILineBreakModeCharacterWrap, 以字符为单位换行(最后⼀一行显
示不完以字符截断剩下的内容不显示也不会省略(没有...))UILineBreakModeClip, 以单词为单位换行(最后⼀一行显示不完
以字符截断剩下的内容不显示也不会省略(没有...)
/////////如下三种会以单词换行和最后一行字符截断会有省略UILineBreakModeHeadTruncation, // 以单词换行,最后一行以字符截
断,最后一行显示不完则省略最后一行的开头,以”...”显示UILineBreakModeTailTruncation, // 以单词换行,最后一行以字
符截断,最后一行显示不完则省略最后一行的中间,以”...”显示UILineBreakModeMiddleTruncation, // 以单词换行,最后一行以
字符截断,最后一行显示不完则省略最后一行的最后,以”...”显示} UILineBreakMode;