UILabel,UITextField 以及UIButton应用

时间:2023-05-07 18:35:32
</pre><pre name="code" class="cpp">一.UILabel 它是ioS开发使用的控件来显示文本,它是UIView子类,所以细节UIView部功能,仅仅只是比UIView多了文字显示的功能,
使用过程也是分四步:
1.创建对象
2.配置属性
3.加入到父视图
4.释放全部权
重点:不同的控件之间仅仅是配置的属性的不同,也就是差异所在,所以学习一个新的控件,仅仅有配置该控件独有的属性就可以
1.创建对象
UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(60 , 234, 200, 100)];
2.设置label上显示的文字
view.text = @"beauy:beauybeauybeauy";
3.设置label上文字的大小
//1.设置字体样式
//2.设置字号
//systemFontOfSize 默认使用系统默认字体,能够更改大小
view.font = [UIFont systemFontOfSize:25];
view.font = [UIFont fontWithName:@"Thonburi-Bold" size:25];
//[UIFont familyNames] 获取字体家族中名称
// NSLog(@"%@",[UIFont familyNames]);
// NSLog(@"%@",[UIFont fontNamesForFamilyName:@"Thonburi"]);
4.字体颜色
view.textColor = [UIColor yellowColor];
5.设置文本的对齐样式
view.textAlignment = NSTextAlignmentCenter;
6.设置文本换行
//假设不限制行数,将值设置为0
view.numberOfLines = 0;
7.换行的标准(文本的截取原则)
view.lineBreakMode = NSLineBreakByWordWrapping;
8.设置阴影的偏移量
view.shadowOffset = CGSizeMake(0, 0);
9. 阴影颜色
view.shadowColor = [UIColor redColor];
二.UITextField 是UIControl的子类,UIControl 又是UIView的子类,所以是一个视图,仅仅只是比UIView多了两个功能:文字显示和文本编辑
// UITextField 的使用步骤和UIView一样
1.创建对象:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 50, 240, 30)];
textField.backgroundColor = [UIColor yellowColor];
2.设置边框样式
textField.borderStyle =UITextBorderStyleRoundedRect;
3.设置默认显示(提示文字)文字,可是不作为文本内容一部分
textField.placeholder = @"手机号/邮箱";
4.设置開始显示文字
textField.text = @"手机号";
5.设置文本颜色
textField.textColor = [UIColor redColor];
6.设置文本对齐方式
textField.textAlignment = NSTextAlignmentCenter;
7.设置文本的字体
//textField.font = [UIFont fontWithName:@"Thonburi" size:35];
8.设置输入框是否可编辑
textField.enabled = YES;
9.设置当開始编辑时是否清除输入框内容
textField.clearsOnBeginEditing = YES;
10.设置password模式,输入框中的内容是否以点的形式显示
textField.secureTextEntry = YES;
11.设置弹出键盘的样式
textField.keyboardType = UIKeyboardTypeASCIICapable;
12.键盘右下角显示的样式
textField.returnKeyType = UIReturnKeyDefault;
13.代理
//代理的使用步骤:1.设置代理 2.服从协议 3.实现协议中的方法
textField.delegate = self;
14.自己定义输入视图(默认键盘)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 , 50, 20, 50)];
textField.inputView = view; [_containView addSubview:textField];
[textField release];
}
//当点击右下角return时会触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//回收键盘,取消第一响应者
[textField resignFirstResponder];
return YES;
} 三.UIButton
UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 400, 220, 40);
button.backgroundColor = [UIColor brownColor];
1.设置圆角
button.layer.cornerRadius = 5;
2.给button加入点击事件
//让target运行action方法,在controlEvents事件发生之后
//click: 后面的參数: 谁调用addTarger:action: 方法,參数就是谁,并且參数仅仅能有一个
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
3.给button设置文字
[button setTitle:@"确认" forState:UIControlStateNormal];
4.改变文字的颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_containView addSubview:button];
}
- (void)click:(UIButton *)button
{
NSLog(@"%@",button);
NSLog(@"雷杰聪贴上");
}

版权声明:本文博主原创文章,博客,未经同意不得转载。