UI day2 UILabel标签 UITextField文本 UIButton按钮

本文详细介绍了iOS开发中UILabel、UITextField及UIButton的使用方法与配置选项,包括文本显示、编辑功能及响应点击事件等关键特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


                          UILabel
1.UILabel(标签)UIView的子类,在UIview基础上扩充了实现显示文字的功能
1.创建控件
2.配置属性
3.添加到父视图上
4.释放所有权
];
1.创建控件
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 300, 60)];

2.配置背景颜色
aLabel.backgroundColor = [UIColor blackColor];

2.1设置文字
aLabel.text = @"hello Lanou hello Lanou hello Lanou hello Lanou hello Lanou ";

2.2设置文本颜色
aLabel.textColor = [UIColor redColor];

2.3设置文本字体
aLabel.font = [UIFont systemFontOfSize:12];

2.4加粗的样式
aLabel.font = [UIFont boldSystemFontOfSize:10]

2.5UIFont是一个字体类,我们可以遍历系统中使用字体名称
for (NSString *name in [UIFont familyNames]) {
        NSLog(@"%@",name);
    }
aLabel.font = [UIFont fontWithName:@"Marion " size:18];

2.6设置文本行数
aLabel.numberOfLines = 0;设置为0 表示不限制行数  默认是一行

2.7行数的截取方式
NSLineBreakByWordWrapping通过单词截取
NSLineBreakByCharWrapping通过字符截取
aLabel.lineBreakMode = NSLineBreakByWordWrapping

2.8设置lable的阴影颜色
aLabel.shadowColor = [UIColor yellowColor];

2.9设置lable的偏移量
aLabel.shadowOffset = CGSizeMake(2, 2) ;
   
2.9设置文本的对齐方式
NSTextAlignmentLeft左对齐
NSTextAlignmentCenter居中对齐
NSTextAlignmentRight右对齐
aLabel.textAlignment = NSTextAlignmentCenter;

2.10 给label切圆角
aLabel.layer.cornerRadius = 10;
aLabel.layer.masksToBounds = YES;

3.添加到父视图上
[contenView addSubview:aLabel];

4.释放所有权
[aLabel release];

                                        UITextField  文本框
1.UITextField  继承的UIControl,是UILabel的基类,扩充了文本编辑的功能,可以允许用户输入或者修改文字

1.创建控件
UITextField  *aTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 30)];
2.配置背景颜色
aTextField.backgroundColor = [UIColor grayColor];

2.2设置文本框的提示文字
aTextField.placeholder = @"请输入文字";

2.3设置输入框文本
aTextField.text = @"兰鸥";

2.4设置文本颜色  只影响输入框输入文本的颜色
aTextField.textColor = [UIColor  redColor];

2.5设置文本输入框对齐方式
aTextField.textAlignment = NSTextAlignmentCenter;

2.6设置文本输入框的是否可编辑  默认是可编辑的
aTextField.enabled = YES;

2.7关闭用户交互的
aTextField.userInteractionEnabled = NO;
   
2.8当文本输入框开始编辑的时候,清空输入框中的内容 默认是NO,只在第一次起作用
aTextField.clearsOnBeginEditing = YES;

2.9设置键盘弹出的格式
aTextField.keyboardType = UIKeyboardTypeEmailAddress;

2.9.1 设置return键的类型
aTextField.returnKeyType = UIReturnKeyEmergencyCall;

2.9.2设置文本输入框是否以加密的形式显示,默认是NO
aTextField.secureTextEntry = YES;

2.10设置文本输入框样式
UITextBorderStyleNone,无边框
UITextBorderStyleLine,线条边框
UITextBorderStyleBezel,倾面边框
UITextBorderStyleRoundedRect圆角边框
aTextField.borderStyle =UITextBorderStyleRoundedRect;

2.11自定义弹出视图
设定自定义的弹出视图后键盘就不在出现了
UIView  *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
inputView .backgroundColor  =[UIColor redColor];
aTextField.inputView = inputView;
[inputView release];
   
2.12自定义键盘上方的辅助视图
UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
accessoryView.backgroundColor = [UIColor greenColor];
aTextField.inputAccessoryView  = accessoryView;

2.13设置文本输入框的清除按钮
UITextFieldViewModeNever,默认
UITextFieldViewModeWhileEditing,当编辑的时候出现清除按钮
UITextFieldViewModeUnlessEditing,当不编辑的时候显示清除按钮
UITextFieldViewModeAlways一直出现清除按钮
aTextField.clearButtonMode  = UITextFieldViewModeAlways;

2.14代理属性
选择代理的条件,能成为代理的对象必须是能拿到源代码的类;(因为我们要在该类中.m实现协议中的方法)
aTextField.delegate = self;
aTextField.tag = 200;

3.添加到父视图上
[contenView addSubview:aTextField];

4.释放所有权
[aTextField  release];


                                               UIButton 按钮
1. UIButton ios中用来响应用户者点击时间的控制是UIControl的子类

1.系统样式
     UIButtonTypeDetailDisclosure详细信息(浅色背景)
     UIButtonTypeContactAdd加号按钮
     IButtonTypeCustom自定义格式,需要添加图片的时候,使用此种类型
     UIButtonTypeDetailDisclosure详细信息(深色背景)
     UIButtonTypeInfoLight详细信息(浅色背景)
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];

2.1配置背景颜色
button.backgroundColor = [UIColor redColor];

2.2设置buttonframe
button.frame =CGRectMake(10, 250, 300, 50);
   
2.3button切圆角
button.layer.cornerRadius = 15;
   
2.4给button添加标题
注意:给button添加标题的一定要写清楚状态
    [button setTitle:@"正常状态" forState:UIControlStateNormal];
    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];

2.5设置button是否可以,默认是可以的YES 设定NO以后button就不可以用了
    [button setTitle:@"不可用状态" forState:UIControlStateDisabled];
     button.enabled  =NO;

2.6设置button是否选中状态  默认处于没有选中状态;设置YES处于选择状态
    [button setTitle:@"选择状态" forState:UIControlStateSelected];
     button.selected = YES;

2.7设置button文字的大小
button一个符合视图(有多个视图构成视图)其中titleLable是一个标题,用来显示标题,还有一个imageView,用来显示图片
button.titleLabel.font = [UIFont systemFontOfSize:20];

2.8设置button标题的颜色
button.tintColor = [UIColor greenColor];

2.9设置button的图片imageView图片
 UIImage是个图片类,继承的NSObject;.png格式的图片不需要后缀,其余格式的图片都要写后缀egjpggif
    UIImage *image = [UIImage imageNamed:@"1"];
    [button setImage:image forState:(UIControlStateNormal)];

2.10设置button的背景颜色
    UIImage *image2 =[UIImage imageNamed:@"2.jpg"];
    [button
setBackgroundImage:image2 forState:(UIControlStateNormal)];
   
   
UIImage *image3 = [UIImage imageNamed:@"3.jpg"];
    [button
setBackgroundImage:image3 forState:(UIControlStateHighlighted)];
   
   
2.11 button的关联事件
target:button指定的响应的对象
action:指定的响应对象调用的方法,方法用来处理button点击事件
events:事件的触发的事件
[button addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
   
3. 添加到父视图
[contenView addSubview:button];

 self.window.backgroundColor = [UIColor whiteColor];
 [self.window makeKeyAndVisible];



#pragma mark 实现UITextFieldDelegate中方法
此方法触发的时机,当我们点击return按钮的自动调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField
resignFirstResponder];

   
return YES;
}

#pragma mark 点击空白处回收键盘
点击屏幕结束的自动走这个方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
   
//取消第一响应者
   
UIView *contentView = [self.window viewWithTag:300];
   
UITextField *field = (UITextField *)[contentView viewWithTag:200];
    [field
resignFirstResponder];
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值