一、UILabel
UILabel:标签视图,继承与UIView,比UIView多的功能是可以显示文字。
UILabel属性设置:
属性名 | 描述 | 示例 |
---|---|---|
text | 要显示的文本内容 | label.text = @”用户名”; |
textColor | 文本内容的颜色 | label.textColor = [UIColor greenColor]; |
textAlignment | 文本的对齐方式(水平方向) | label.textAlignment = NSTextAlignmentCenter; |
font | 文本字体 | label.font = [UIFont fontWithName:@”Zapfino” size:8]; |
numberOfLines | 行数 | label.numberOfLines = 3;//显示3行,注意label的高度要能容纳3行,如果3行没能显示完信息,没显示的信息以省略号代替.自动换行设为0. |
lineBreakMode | 断行模式 | label.lineBreakMode = NSLineBreakByCharWrapping;//以单词为单位换行 |
shadowColor | 阴影颜色 | label.shadowColor = [UIColor redColor]; |
shadowOffset | 阴影大小 | label.shadowOffset = CGSizeMake(2, 1);//阴影向x正方向偏移2,向y正方向偏移1 |
二、UITextField
UITextField(输入框):是控制文本输入和显示的控件。UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑,UITextField允许用户编辑文字(输入)。
UITextField文本显示属性:
属性名 | 描述 | 示例 |
---|---|---|
text | 要显示的文本内容 | tf.text = @”这是一个输入框”; |
textColor | 文本内容的颜色 | tf.textColor = [UIColor redColor]; |
textAlignment | 文本的对齐方式(水平方向) | tf.textAlignment = NSTextAlignmentLeft; |
font | 文本字体 | tf.font = [UIFont fontWithName:@”Helvetica-Bold” size:20];//黑体加粗,20号字 |
placeholder | 占位字符串(没有任何输入时,给出的提示字符串) | tf.placeholder = @”请输入6位密码”; |
UITextField输入控制属性:
属性名 | 描述 | 示例 |
---|---|---|
enabled | 是否允许输入 | tf.enabled = NO;//不允许输入,不弹出键盘,默认是YES,允许输入 |
clearsOnBeginEditing | 是否开始输入的时候清空输入框内容 | tf.clearsOnBeginEditing = YES;//YES,清空;NO,不清空 |
secureTextEntry | 是否文字以圆点格式显示 | tf.secureTextEntry = YES;//YES,密码模式;NO,普通模式 |
keyboardType | 键盘右下角return按钮类型(枚举值) | tf.keyboardType = UIKeyboardTypeNumberPad;//数字键盘 |
returnKeyType | 键盘右下角return按钮类型(枚举值) | tf.returnKeyType = UIReturnKeyDone; |
inputView | 自定义输入视图(默认是键盘) | tf.inputView = keyBoard; |
inputAccessoryView | 输入视图上方的辅助视图(默认nil) | tf.inputAccessoryView = accessoryView; |
UITextField外观控制属性:
属性名 | 描述 | 示例 |
---|---|---|
borderStyle | 边框样式(枚举值) | tf.borderStyle = UITextBorderStyleRoundedRect; |
clearButtonMode | 清除按钮模式(枚举值) | tf.clearButtonMode = UITextFieldViewModeAlways;//总是显示清除按钮 |
leftView | 输入框左视图 | tf.leftView = leftView; |
leftViewMode | 左视图的显示模式 | tf.leftViewMode = UITextFieldViewModeAlways;//总是显示左视图 |
rightView | 输入框视图 | tf.rightView = rightView; |
rightViewMode | 右视图的显示模式 | tf.rightViewMode = UITextFieldViewModeAlways; |
UITextField常用代理方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
三、UIButton
UIButton(按钮):是响应用户点击的控件,侧重于处理用户交互事件。
UIButton外观控制属性:
方法名 | 描述 | 示例 |
---|---|---|
setImage:forState: | 设置指定状态下的前景图片 | [btn setImage:[UIImage imageNamed:@”1.jpg”] forState:UIControlStateNormal]; |
imageForState | 获取指定状态下的前景图片 | UIImage *img = [btn imageForState:UIControlStateNormal]; |
setBackgroundImage:forState: | 设置指定状态下的背景图片 | [btn setBackgroundImage:[UIImage imageNamed:@”1.jpg”] forState:UIControlStateNormal]; |
backgroundImageForState: | 获取指定状态下的背景图片 | UIImage *bgImage = [btn backgroundImageForState:UIControlStateNormal]; |
setTitle:forState: | 设置指定状态下的标题 | [btn setTitle:@”点我我~” forState:UIControlStateNormal]; |
titleForState | 获取指定状态下的标题 | NSString *str = [btn titleForState:UIControlStateNormal]; |
setTitleColor:forState: | 设置指定状态下的标题颜色 | [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; |
titleColorForState: | 获取指定状态下的标题颜色 | UIColor *titleColor = [btn titleColorForState:UIControlStateNormal]; |
setTitleShadowColor:forState: | 设置指定状态下的标题阴影颜色 | [btn setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal] |
titleShadowColorForState: | 获取指定状态下的标题阴影颜色 | UIColor *yinColor = [btn titleShadowColorForState:UIControlStateNormal]; |
UIButton添加事件:
方法名 | 描述 | 示例 |
---|---|---|
addTarget:action: forControlEvents: | 为按钮添加事件,指定按钮点击之后,执行target的action方法 | [btn addTarget:self action:@selector(handleBtn:) forControlEvents:UIControlEventTouchUpInside]; |
removeTarget:action:forControlEvents: | 移除按钮的点击事件 | [btn removeTarget:self action:@selector(handleBtn:) forControlEvents:UIControlEventTouchUpInside]; |
四、UIImageView
UIImageView:是iOS中用于显示图片的类,相当于画框;UIImage,非视图,只有通过UIImageView才能显示,相当于照片。
创建image:
//创建image方式1, 该方式会把图片存入内存.
UIImage *image = [UIImage imageNamed:@"1.jpg"];
//创建image方式2 不会存入内存,直接从包内读取.
UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"]];
UIImageView的动态图示例:
UIImageView *imageV = [[UIImageView alloc] init];
NSMutableArray *imageArr = [@[] mutableCopy];
for (int i = 0; i <= 21; i++)
{
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", i] ofType:@"png"]];
[imageArr addObject:image];
}
//设置一组动态图片
imageV.animationImages = imageArr;
//整个动画的执行事件.
imageV.animationDuration = 0.5;
//整个动画需要循环多少次
imageV.animationRepeatCount = 10000;
//开始动画
[imageV startAnimating];
//结束动画
//[imageV stopAnimating];