基础控件
1、UILabel 常见的属性
@property(nonatomic,copy) NSString *text; // 显示的文字 @property(nonatomic,retain) UIFont *font; // 字体 { // UIFont代表字体,常见创建方法有以下几个: + (UIFont *)systemFontOfSize:(CGFloat)fontSize; 系统默认字体 + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize; 粗体 + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize; 斜体 } @property(nonatomic,retain) UIColor *textColor; // 文字颜色 @property(nonatomic) NSTextAlignment textAlignment; // 对齐模式(比如左对齐、居中对齐、右对齐) @property(nonatomic) NSInteger numberOfLines; // 文字行数 (0 == 多行 默认 == 1) @property(nonatomic) NSLineBreakMode lineBreakMode; // 换行模式
## contentMode属性
- 带有scale单词的:图片有可能会拉伸
- UIViewContentModeScaleToFill
- 将图片拉伸至填充整个[]imageView
- 图片显示的尺寸跟imageView的尺寸是一样的
- 带有aspect单词的:保持图片原来的宽高比
- UIViewContentModeScaleAspectFit
- 保证刚好能看到图片的全部
- UIViewContentModeScaleAspectFill
- 拉伸至图片的宽度或者高度跟imageView一样
- 没有scale单词的:图片绝对不会被拉伸,保持图片的原尺寸
## 小语法点
- 不能直接修改:OC对象的结构体属性的成员
- 下面的写法是错误的
imageView.frame.size = imageView.image.size;
- 正确写法
CGRect tempFrame = imageView.frame; tempFrame.size = imageView.image.size; imageView.frame = tempFrame;
## initWithImage:方法
- 利用这个方法创建出来的imageView的尺寸和传入的图片尺寸一样
## 修改frame的3种方式
- 直接使用CGRectMake函数
imageView.frame = CGRectMake(100, 100, 200, 200); // - 利用临时结构体变量 CGRect tempFrame = imageView.frame; tempFrame.origin.x = 100; tempFrame.origin.y = 100; tempFrame.size.width = 200; tempFrame.size.height = 200; imageView.frame = tempFrame; // - 使用大括号{}形式 imageView.frame = (CGRect){{100, 100}, {200, 200}};
- 抽取重复代码
- 将相同代码放到一个新的方法中
- 不用的东西就变成方法的参数
- 图片的加载方式
- 有缓存
UIImage *image = [UIImage imageNamed:@"图片名"];
- 使用场合:图片比较小、使用频率较高
- 建议把需要缓存的图片直接放到Images.xcassets
- 无缓存
NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片的扩展名"]; UIImage *image = [UIImage imageWithContentsOfFile:@"图片文件的全路径"];
- 使用场合:图片比较大、使用频率较小
- 不需要缓存的图片不能放在Images.xcassets
- 放在Images.xcassets里面的图片,只能通过图片名去加载图片
@property(nonatomic,retain) UIImage *image; // 显示的图片 @property(nonatomic,copy) NSArray *animationImages; // 显示的动画图片 @property(nonatomic) NSTimeInterval animationDuration; // 动画图片的持续时间 @property(nonatomic) NSInteger animationRepeatCount; // 动画的播放次数(默认是0,代表无限播放)
- (void)startAnimating; // 开始动画 - (void)stopAnimating; // 停止动画 - (BOOL)isAnimating; // 是否正在执行动画
## 渐变动画
- 方式1:头尾式
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2.0]; /* 需要执行动画的代码 */ [UIView commitAnimations];
- 方式2:block式
[UIView animateWithDuration:2.0 delay:1.0 options:kNilOptions animations:^{ /* 需要执行动画的代码 */ } completion:nil] // 1s后,再执行动画(动画持续2s)
## 按钮
- 自定义按钮:调整内部子控件的frame
- 方式1:实现titleRectForContentRect:和imageRectForContentRect:方法,分别返回titleLabel和imageView的frame
- 方式2:在layoutSubviews方法中设置
- 内边距
// 设置按钮内容的内边距(影响到imageView和titleLabel) @property(nonatomic) UIEdgeInsets contentEdgeInsets; // 设置titleLabel的内边距(影响到titleLabel) @property(nonatomic) UIEdgeInsets titleEdgeInsets; // 设置imageView的内边距(影响到imageView) @property(nonatomic) UIEdgeInsets imageEdgeInsets;
## 图片拉伸
- iOS5之前
// 只拉伸中间的1x1区域
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- iOS5开始
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets; - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
3.1 常见的状态
normal(普通状态) // 默认情况(Default) //对应的枚举常量:UIControlStateNormal highlighted(高亮状态) // 按钮被按下去的时候(手指还未松开) // 对应的枚举常量:UIControlStateHighlighted disabled(失效状态,不可用状态) // 如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击 // 对应的枚举常量:UIControlStateDisabled
3.2 设置按钮的背景图片
(为了保证高亮状态下的图片正常显示,必须设置按钮的type为custom)
3.3 按钮的样式
(可自己去storyboard 自己挨个试验下~~~)
3.4 常见设置
- (void)setTitle:(NSString *)title forState:(UIControlState)state; // 设置按钮的文字 - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state; // 设置按钮的文字颜色 - (void)setImage:(UIImage *)image forState:(UIControlState)state; // 设置按钮内部的小图片 - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state; // 设置按钮的背景图片 (宽高全部填充) // 设置按钮的文字字体(需要拿到按钮内部的label来设置) btn.titleLabel.font = [UIFont systemFontOfSize:13]; - (NSString *)titleForState:(UIControlState)state; // 获得按钮的文字 - (UIColor *)titleColorForState:(UIControlState)state; // 获得按钮的文字颜色 - (UIImage *)imageForState:(UIControlState)state; // 获得按钮内部的小图片 - (UIImage *)backgroundImageForState:(UIControlState)state; // 获得按钮的背景图片
// 创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 默认状态的背景 [btn setBackgroundImage:[UIImage imageNamed:@"****"] forState:UIControlStateNormal]; // 默认状态的文字 [btn setTitle:@"****" forState:UIControlStateNormal]; // 默认状态的文字颜色 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
总结:UIButton 、UIImageView 、UILabel 的选择
UIImageView
UILabel
行号 = 商品的索引 / 3
列号 = 商品的索引 % 3