IOS基本控件的基本使用
IOS最常见的三个控件:UIButton UILabel UIImageView
一.UIBUtton
按钮类型:
1. UIButtonTypeCustom //按钮的内容需要自定义
2. UIButtonTypeRoundedRect //圆角矩形按钮
3. UIButtonTypeDetailDisclosure //显示明细按钮
4. UIButtonTypeInfoLight //亮色信息按钮,用于深色背景
5. UIButtonTypeInfoDark //深色信息按钮,用于浅色背景
6. UIButtonTypeContactAdd //添加按钮
按钮文字:
1.正常状态下按钮文字
[button setTitle:@"hello" forState:UIControrStateNormal];
2.长按状态(高亮)下按钮文字
[button setTitle:@"world" forState:UIControlStateHighlighted];
按钮文字颜色:
1.正常状态下按钮文字颜色
[button setTitleColor:[UIColor redColor] forState:UIControrStateNormal];
2.长按状态(高亮)下按钮文字颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
//设置文字与文字颜色时都需要设置其状态
按钮背景颜色:
[button setBackgroundColor:[UIColor blackColor]];
//设置按钮背景颜色时,需要按钮是自定义样式
按钮设置背景及设置图像:
// 加载图像
UIImage *image = [UIImage imageNamed:@"sub_black_add.png"];
// 设置按钮图像
[button setImage:image forState:UIControlStateNormal];
// 设置按钮背景图像
[button setBackgroundImage:image forState:UIControlStateNormal];
注:1.按钮设置的背景图片会根据按钮的大小拉伸
2.按钮设置的图片会自动居中
3.同时设置了文字与图片时:如果按钮足够大,则文字与图片并列显示
如果按钮不够大,优先显示图片
按钮设置监听事件:UILabel
[button addTarget:self action:@selector(tapButton)forControlEvents:UIControlEventTouchUpInside];
//监听方法中只可以有一个参数
二.UILabel(基本设置与UIBUtton相类似,下面只介绍比较些比较特殊的设置)
设置文字:
//设置标题
[label setText:@"hello"];
//设置字体和大小
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:40]];
设置文字阴影和与颜色:
// 设置背景色
[label setBackgroundColor:[UIColor greenColor]];
// 设置文本的颜色
[label setTextColor:[UIColor whiteColor]];
// 设置文本的阴影色彩和透明度
[label setShadowColor:[UIColor colorWithWhite:0.1f alpha:0.8f]];
// 设置阴影的偏移量
[label setShadowOffset:CGSizeMake(2.0f, 2.0f)];
设置文字对齐方式与换行:
//设置对齐方式
[label setAlignment:NSTextAlignmentCenter];
// 换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。
// 指定换行模式
[label setLineBreakMode:NSLineBreakByWordWrapping];
// 指定label的行数,为0时没有最大行数限制
[label setNumberOfLines:2];
设置label形变参数:
// 设置label的旋转角度
[label setTransform:CGAffineTransformMakeRotation(M_PI_4)];
三.UIImageView(只介绍特有的属性)
设置图片:
[self.imageView setImage:[UIImage imageNamed:@"abc.png"]];
设置图片显示模式:
//设置为居中显示并保持原来的宽高比
imageView.contentMode = [UIViewContentModeScaleAspectFit];
设置动画:
UIImageView可以让一系列的图片在特定的时间内按顺序显示
属性说明:
animationImages:要显示的一组图片序列
animationDuration:完整地显示所有图片所需的时间
animationRepeatCount:动画的执行次数(默认为0,代表无限循环)
相关方法:
- (void)startAnimating; 开始动画
- (void)stopAnimating; 停止动画
- (BOOL)isAnimating; 是否正在运行动画