04-UI控件概览



05-基础控件


UILabel的常见属性

@property(nonatomic,copy)   NSString           *text; 
显示的文字

@property(nonatomic,retain) UIFont             *font; 
字体

@property(nonatomic,retain) UIColor            *textColor; 
文字颜色

@property(nonatomic)        NSTextAlignment    textAlignment; 
对齐模式(比如左对齐、居中对齐、右对齐) 

@property(nonatomic) NSInteger numberOfLines; 
文字行数

@property(nonatomic)        NSLineBreakMode    lineBreakMode;

UIFont

UIFont代表字体,常见创建方法有以下几个:
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;   系统默认字体
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;  粗体
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;  斜体

UIImageView的常见属性

@property(nonatomic,retain) UIImage *image; 
显示的图片

@property(nonatomic,copy) NSArray *animationImages; 
显示的动画图片

@property(nonatomic) NSTimeInterval animationDuration; 
动画图片的持续时间

@property(nonatomic) NSInteger      animationRepeatCount; 
动画的播放次数(默认是0,代表无限播放)

UIImageView的常见方法

- (void)startAnimating; // 开始动画
- (void)stopAnimating; // 停止动画
- (BOOL)isAnimating; // 是否正在执行动画

UIImage

一个UIImage对象代表一张图片,一般通过imageNamed:方法就可以通过文件名加载项目中的图片

UIImage *image = [UIImage imageNamed:@"lufy"];

在用代码创建按钮的同时指定按钮样式
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIButtonTypeCustom:无类型,按钮的内容需要自定义
UIButtonTypeDetailDisclosure: 
UIButtonTypeInfoLight: 
UIButtonTypeInfoDark: 
UIButtonTypeContactAdd:

UIButton的常见设置

- (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;
设置按钮的背景图片

UIButton的常见设置

设置按钮的文字字体(需要拿到按钮内部的label来设置)
btn.titleLabel.font = [UIFont systemFontOfSize:13];

- (NSString *)titleForState:(UIControlState)state; 
获得按钮的文字

- (UIColor *)titleColorForState:(UIControlState)state;
获得按钮的文字颜色

- (UIImage *)imageForState:(UIControlState)state;
获得按钮内部的小图片

- (UIImage *)backgroundImageForState:(UIControlState)state;
获得按钮的背景图片



解析Plist文件

接下来通过代码来解析Plist文件中的数据
获得Plist文件的全路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"shops" ofType:@"plist"];

加载plist文件
_shops = [NSArray arrayWithContentsOfFile:path];


懒加载

接下来通过代码来解析Plist文件中的数据
获得Plist文件的全路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"shops" ofType:@"plist"];

加载plist文件
_shops = [NSArray arrayWithContentsOfFile:path];

用模型取代字典的好处

使用字典的坏处
一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编辑器没有智能提示,需要手敲
dict[@"name"] = @"Jack";
NSString *name = dict[@"name"];
手敲字符串key,key容易写错
Key如果写错了,编译器不会有任何警告和报错,造成设错数据或者取错数据
使用模型的好处
所谓模型,其实就是数据模型,专门用来存放数据的对象,用它来表示数据会更加专业
模型设置数据和取出数据都是通过它的属性,属性名如果写错了,编译器会马上报错,因此,保证了数据的正确性
使用模型访问属性时,编译器会提供一系列的提示,提高编码效率
app.name = @"Jack";
NSString *name = app.name;

字典转模型

字典转模型的过程最好封装在模型内部

模型应该提供一个可以传入字典参数的构造方法
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)xxxWithDict:(NSDictionary *)dict;

instancetype

instancetype在类型表示上,跟id一样,可以表示任何对象类型

instancetype只能用在返回值类型上,不能像id一样用在参数类型上

instancetype比id多一个好处:编译器会检测instancetype的真实类型



Xib的加载

方法1
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"xib文件名" owner:nil options:nil]

方法2
UINib *nib = [UINib nibWithNibName:@"xib文件名" bundle:nil];
NSArray *views = [nib instantiateWithOwner:nil options:nil];


常用的Xcode插件

Xcode插件大全
http://www.cocoachina.com/industry/20130918/7022.html

必备
文档注释生成:https://github.com/onevcat/VVDocumenter-Xcode
自动检索图片名:https://github.com/ksuther/KSImageNamed-Xcode
插件管理工具:https://github.com/mneorr/Alcatraz

移除插件(可以使用上面提到的插件管理工具Alcatraz)
到~/Library/Application Support/Developer/Shared/Xcode/Plug-ins文件夹中删除

插件失效修复:http://joeshang.github.io/2015/04/10/fix-xcode-upgrade-plugin-invalid/


06-顏色認知




results matching ""

    No results matching ""