基础控件

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;

// 换行模式

 

2、UIImageView
 * UIImageView极其常用,功能比较专一:显示图片
 * 常见的图片的填充模式
    1,AspectFit 高填充,宽等比缩放
    2,AspectFill 宽填充,高等比缩放
    3,ToFill  宽高都填充(很难看~~~)

## 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里面的图片,只能通过图片名去加载图片

 2.1 常见属性
@property(nonatomic,retain) UIImage *image;
//  显示的图片
@property(nonatomic,copy) NSArray *animationImages;

//  显示的动画图片 
@property(nonatomic) NSTimeInterval animationDuration;

//  动画图片的持续时间
@property(nonatomic) NSInteger      animationRepeatCount;

 // 动画的播放次数(默认是0,代表无限播放)

 

2.2 常见方法
- (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、按钮
 
  l还有一个非常重要的UI控件---UIButton,俗称“按钮”
  l一般情况下,点击某个控件后,会做出相应反应的都是按钮
  l按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置

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;
// 获得按钮的背景图片

 

 
storyboard到代码的转换:
// 创建一个自定义的按钮

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

// 默认状态的背景

[btn setBackgroundImage:[UIImage imageNamed:@"****"] forState:UIControlStateNormal];

// 默认状态的文字

[btn setTitle:@"****" forState:UIControlStateNormal];

// 默认状态的文字颜色

[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

 

总结:UIButton 、UIImageView 、UILabel 的选择

特点
UIButton
-既能显示文字,又能显示图片(能显示2张图片,背景图片、内容图片)
-长按高亮的时候可以切换图片\文字
-直接通过addTarget...方法监听点击

 UIImageView

-能显示图片,不能直接通过addTarget...方法监听点击

UILabel

-能显示文字,不能直接通过addTarget...方法监听点击

 

选择
仅仅是显示数据,不需要点击
-建议选择UIImageView、UILabel
-
不仅显示数据,还需要监听点击
-建议选择UIButton
-其实UIImageView、UILabel也可以通过手势识别器来监听(后面课程会学)
-
长按控件后,会改变显示的内容
-不用考虑了,选择UIButton(因为UIButton有highlighted这种状态)
-
同时显示2张图片:背景图片、内容图片
-不用考虑了,选择UIButton
 
 
~~~~   九宫格思路
计算行号、列号

行号 = 商品的索引 / 3

列号 = 商品的索引 % 3

posted @ 2015-11-12 15:28  一介闲人  阅读(155)  评论(0编辑  收藏  举报