一 UILable(继承自 UIView, 在UIView 的基础上进行扩充)
1⃣ 创建 UILable 控件
UILable*lable = [[UILable alloc]initWithFrame:CGRectMake(20,30,100,50)];
[self.WindowaddSubview:lable]; 添加到父视图
[lablerelease]; 释放
2⃣ 添加背景颜色(backgroundColor)
lable.backgroundColor= [UIColor redColor];
3⃣ 添加文本内容(text)
lable.text= @”蓝欧,欢迎你!!”;
4⃣ 设置字体大小(font)
lable.font= [UIFont systemFontOfSize:20]; 系统字体大小
lable.font= [UIFont boldSystemFontOfSize:20] 系统加粗字体大小
lable.font= [UIFont fontWithName:@”Tamil Sangam MN”size:20]; 系统字体设置
遍历系统的字体
NSArray*array = [UIFont familyNames];
For(NSString*str in array){
NSLog(@”%@”,str);
}
5⃣ 设置文本显示行数,行数为0时,不限制行数
lable.numberOfLines= 0;
6⃣ 设置文本换行时的截断字符串
lable.lineBreakMode= NSLineBreakCharWrapping;(枚举值,截断字符串)
7⃣ 设置高亮
lable.highlighted= YES;
8⃣ 设置高亮颜色
lable.highlightedTextColor= [UIColor greenColor];
9⃣ 设置字体颜色
lable.textColor= [UIColor blueColor];
设置文本对其方式
lable.textAlignment= NSTextAlignmentCenter;
设置引用颜色
lable.shadowColor= [UIColor yellowColor];
设置阴影偏移量(阴影的偏移量是根据 x,y的值进行确定的)
lable.shadowOffset= CGSizeMake(5,0) (x为正,向右; y为正,向下.x为负,向左;y 为负,向上 )
二 UITextFiled(继承自UIControl)
1⃣ 继承自 UIControl, 是在 UILable 的基础上,多加了一个文本编辑功能,可以允许用户输入以及修改文本
2⃣ 创建文本框
UITextField*filed = [[UITextFiled alloc]initWithFrame:CGRectMake(10,100,300,50)];
3⃣对文本进行标记,方便后面的取值
filed.tag= 100;
4⃣ 设置背景颜色
filed.backgroundColor= [UIColor yellowColor];
5⃣使用占位符
filed.placeholder= @”请输入密码”;
6⃣显示文本
filed.text= @”你好”;
7⃣ 设置是否清空按钮格式
filed.clearButtonMode= UITextFiledViewModeAlways;
8⃣ 文本颜色
filed.textColor= [UIColor redColor];
9⃣对齐方式
filed.textAlignment= NSTextAlignmentCenter;
2.设置属性
1⃣ 设置边框
filed.borderStyle= UITextBorderStyleRoundedRect;
filed.enabled= NO; 默认是 YES
2⃣当开始输入的时候,是否清空文本框内容
filed.clearsOnBeginEditing= YES;默认为0
3⃣设置 return 的按钮样式
filed.returnKeyType= UIReturnKeySearch;
4⃣设置输入框的文字以密码模式显示
filed.secureTextEntry= YES;
5⃣设置键盘的格式
filed.keyboardType= UIKeyboardTypeNumberPad;
6⃣自定义弹出视图框
UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,250)];
View.backgroundColor= [UIColor cyanColor];
Filed.inputView= view;
[viewrelease];
3.自定义属性
1⃣自定义键盘上方的弹出视图
UIView *viewFuzhu = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
viewFuzhu.backgroundColor = [UIColorcyanColor];
filed.inputAccessoryView = viewFuzhu;
[viewFuzhu release];
2⃣设置文本框的代理
针对某一个类的代理的命名规则:类名+ delegate
当一个类的一个属性是遵循了某个协议,它的命名:delegate
delegate 的属性语义特性使用 assign
要遵循代理的这个类,必须是已知类,(因为我们要在遵循代理的. M文件中实现协议的方法)
filed.delegate= self;
实现代理的方法:
-(BOOL)textFieldShouldReturn:(UITextFiled *)textFiled{
回收键盘功能
[textFiledresignFirstResponder];
returnYES;
}
3⃣使用点击事件的方法,回收键盘
- (void)touchesBegan: (NSSet *)toucheswithEvent: (UIEvent *)event{
UITextField *view =(UITextField *)[self.window viewWithTag;100];
[view resignFirstResponder];
}
三 UIButton按钮属性和事件
1. UIButton继承于 UIControl, 主要是用来处理点击事件的控件
2. 1⃣创建按钮控件
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
注意:创建按钮的时候,使用便利构造器方法,不用手动释放内存
2⃣设置背景颜色
button.backgroundColor =[UIColor yellowColor];
3⃣设置位置和大小
button.frame =CGRectMake(20,100,280,50);
4⃣ 显示文字
[button setTitle:@”登陆” forState:UIControlStateNormal];
[button setTitle:@”高亮状态” forState:UIControlStateHighlighted];
button.enabled = NO;
[button setTitle;@”不可用状态” forState:UIControlStateDisabled];
button.selected = YES;
[button setTitle:@”选择状态” forState:UIControlStateSelected];
5⃣设置标题的颜色
[button setTitleColor:[UIColorredColor] forState:UIControlStateHighlighted];
3. 设置文字大小(button 是一个复合视图,是由多个视图构成的,其中显示 title 的是标题 titlelable, 设置标题的属性都是在titleable 上操作的)
1⃣设置文字大小
button.titleLabel.font =[UIFont systemFontOfSize:20];
2⃣ 给标题添加颜色
button.tintColor = [UIColorredColor];
3⃣ 给按钮设置图片
UIImage, 图片类,继承自 NSObject,*.png 格式的图片可以省略后缀,其他格式的必须加上后缀
UIImage *image= [UIImageimageNamed:@”button”];
下面的这个添加图片的方式, 会跟文字争夺地盘
[buttonsetImage:image forState:UIControlStateNormal];
[buttonsetBackgroundImage:image forState:UIControlStateNormal];
4. UIButton的关联事件
[button addTarget:self action:@selector(butClick)forControlEvents:UIControlEventTouchUpInside];
target: 指定的响应对象 action:指定对象的响应方法 forControlEvents: 事件的触发时机
处理事件可以跟参数,只能跟一个,帮谁处理的事件,就跟谁的数据类型
5. 收回键盘代理事件
- (BOOL)textFieldShouldReturn:(UITextField*)textField{
[ textField resignFirstResponder];
returnYES;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
UITextField *view= (UITextField *)[self.windowviewWithTag:101];
[view resignFirstResponder];
}