UI控件-UILabel ,UIButton,UITextField的详解
一,UILabel文本标签
UILabel主要用来显示简短的文本
UILabel常用属性:
1.shadowColor
属性:设置阴影颜色 。
2.shadowOffset
属性:设置阴影偏移量。
3.lineBreakMode
属性:设置文字过长时的显示格式。
4.attributedText
属性:设置标签属性文本。
5.highlightedTextColor
属性:设置高亮显示时的文本颜色。
6.highlighted
属性:设置是否高亮显示。
7.userInteractionEnabled
属性:设置是否能与用户交互。
8.enabled
属性:只是决定了Label的绘制方式,将它设置为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
9.numberOfLines
属性:设置文本最多行数,为0时没有最大行数限制
UIlabel 部分代码实现:
//-,UILabel //1.创建 UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 200, 0)]; textlabel.backgroundColor = [UIColor yellowColor]; //2.文字,默认为空 textlabel.text = @"Copyright © 2016年 fanghao. All rights reserved"; //3.文字颜色,默认为黑色 textlabel.textColor = [UIColor redColor]; //4.文字样式和大小 默认为system样式,默认大小为17 textlabel.font = [UIFont systemFontOfSize:40]; //5.对齐方式 textlabel.textAlignment = NSTextAlignmentCenter; //6.设置行数,默认为1,如果为0自动换行 textlabel.numberOfLines = 0; //7.断句,只显示不在类容时才生效 textlabel.lineBreakMode = NSLineBreakByTruncatingTail; //8.自动改变高度来适应文本的大小 [textlabel sizeToFit]; //9.设置阴影颜色 textlabel.shadowColor = [UIColor blackColor]; //10.设置阴影偏移量 textlabel.shadowOffset = CGSizeMake(1, 1); //11.富文本 NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:textlabel.text attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:28],NSStrikethroughColorAttributeName:[UIColor whiteColor],NSUnderlineColorAttributeName:@(NSUnderlineStyleSingle |NSUnderlinePatternDashDotDot)}]; textlabel.attributedText = attributedString; //textlabel.transform = CGAffineTransformRotate(textlabel.transform,M_PI/18); //布局layer //边框圆角 textlabel.layer.cornerRadius = 5; //边框宽度 textlabel.layer.borderWidth = 2; //边框颜色 textlabel.layer.borderColor = [[UIColor orangeColor]CGColor]; [self.view addSubview:textlabel]; //自动改变高度来适应文本的大小 [textlabel sizeToFit];
二.UIButton
UIButton按钮,主要用来响应用户点击事件 .
常用属性
1.buttonType 按钮显示样式类型
2.titleLabel 按钮标题文本Label对象
3.contentEdgeInsets
设置调整按钮的边间距---整个内容的边间距
4.titleEdgeInsets
设置调整按钮的边间距---标题设置。
5.imageEdgeInsets
设置调整按钮的边间距---图片设置。
6.reversesTitleShadowWhenHighlighted
设置按钮高亮时是否改变阴影.默认时NO。
button有很多种状态,不同的状态可以对应不同的按钮图片
UIButton状态:
UIControlStateNormal //正常状态
UIControlStateHighlighted //高亮状态
UIControlStateDisabled //禁用状态
UIControlStateSelected //选中状态
UIControlStateApplication //
UIControlStateReserved // 系统保留状态
UIButton 的一些方法 - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state; - (void)setTitleShadowColor:(UIColor *)color forState: (UIControlState)
UIButton 部分代码实现:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //1.创建 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(50, 59, 200, 60); button.backgroundColor = [UIColor yellowColor]; [self.view addSubview:button]; //设置标题 [button setTitle:@"按钮" forState:UIControlStateNormal]; [button setTitle:@"选中按钮" forState:UIControlStateSelected]; //设置标题色 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ]; [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; //设置字体 button.titleLabel.font = [UIFont boldSystemFontOfSize:20]; //设置图片,与文字不同时展示 [button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; //设置背景图片 [button setBackgroundImage:[UIImage imageNamed:] forState:<#(UIControlState)#>] //添加事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
文本输入框,用于输入少量文字
常用属性:
1.autocapitalizationType 自动大写类型,可禁止。
2.keyboardType 键盘类型
3.borderStyle 输入框显示样式
4.returnKeyType 键盘上的return键的显示样式
5.clearButtonMode 清除按钮模式
6.placeholder 输入框为空显示的提示文字
7.secureTextEntry 是否提供文本隐藏特性(密码输入框
8.textAlignment 输入框中的文本对齐方式
9.textColor 输入框中的文本颜
10.font 输入框的文本字体
11.text 输入框的文本
UITextField 代码实现
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //1.创建 UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 55, 200, 40)]; textFiled.backgroundColor = [UIColor redColor]; [self.view addSubview:textFiled]; /// textFiled.autocapitalizationType = UITextAutocapitalizationTypeNone; ////请输入文字 textFiled.placeholder = @"请输入"; //键盘类型 textFiled.keyboardType = UIKeyboardTypeURL; //键盘上return 的样式 textFiled.returnKeyType = UIReturnKeyDone; //边框类型 textFiled.borderStyle = UITextBorderStyleRoundedRect; //是否为密码输入 textFiled.secureTextEntry = NO; //清除按钮的模式 textFiled.clearButtonMode = UITextFieldViewModeAlways; //设置协议 textFiled.delegate = self; }
UITextField的委托方法
#pragma mak - uiTextFieldDelegate //将要开始输入时调用 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSLog(@"开始输入"); return YES; } //将要输入结束时调用 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { NSLog(@"输入结束"); return YES; } //清除文字按钮点击事件 - (BOOL)textFieldShouldClear:(UITextField *)textField { NSLog(@"清除输入内容了"); return YES; } //键盘上的return按钮 - (BOOL)textFieldShouldReturn:(UITextField *)textField { //隐藏输入键盘 //1.通过视图强制结束 [self.view endEditing:YES]; //2.通过自己主动结束 //[textField resignFirstResponder]; return YES; }