重要知识点
UIView子类:UIWindow, UILable, UIProgressView, UIImageView, UIAlertView, UIActionSheet, UIControl
UIControl子类:UIButton, UITextField, UISlider, UISwitch
UILable(文本标签)
创建
因为是UIView的子类,创建的方式和UIView的一样。
属性
@property(nullable, NOnatomic,copy) NSString *text; //文本内容
@property(null_resettable, NOnatomic,strong) UIFont *font; //文本字体
@property(null_resettable, NOnatomic,strong) UIColor *textColor; //文本颜色
@property(NOnatomic) CGSize shadowOffset; //文本阴影偏移量
@property(NOnatomic) NSTextAlignment textAlignment; //文本对齐方式
@property(NOnatomic) NSLineBreakMode lineBreakMode; //超出文本显示方式
@property(nullable, NOnatomic,copy) NSAttributedString *attributedText; //富文本
@property(nullable, NOnatomic,strong) UIColor *highlightedTextColor; //高亮文本颜色
@property(NOnatomic) NSInteger numberOfLines; //文本显示行数
UIImageView(图片视图,用于将UIImage显示在屏幕上)
UIImage(图像)
UIImage是NSObject的子类
UIImage代表了显示图像所需要的数据
UIImage对应UIKit中的UIImageView
UIImage常用属性:size(图片的大小),scale(图片的比例,可以看出图片是一倍图还是二倍图)
UIImage并不能直接显示,如果想要在屏幕上显示图片,需要用UIImageView
UIImage常用方法:
+ (nullable UIImage *)imageNamed:(NSString *)name; //存放在Assets.xcassets中采用此方法加载图片
+ (nullable UIImage *)imageWithContentsOfFile:(NSString *)path; //根据路径加载图片 类方法
+ (nullable UIImage *)imageWithData:(NSData *)data; //根据NSDate类型加载图片 类方法
+ (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;
- (nullable instancetype)initWithContentsOfFile:(NSString *)path; //根据路径加载图片 实例方法
- (nullable instancetype)initWithData:(NSData *)data; //根据NSDate类型加载图片 实例方法
常用属性
@property (nullable, NOnatomic, strong) UIImage *image; //图片数据
@property (nullable, NOnatomic, strong) UIImage *highlightedImage; //高亮时显示的图片数据
@property (nullable, NOnatomic, copy) NSArray<UIImage *> *animationImages; //动画图片,图片组
初始化方法
- (instancetype)initWithImage:(nullable UIImage *)image;
- (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage;
UIControl(响应用户事件,如:点击,拖动)
UIKit提供了一组控件:UISwitch(开关),UIButton(按钮),UISegmentedControl(分段控件),UISlider(滑块),UITextField(文本字段)等
这些控件的基类均是UIControl,而UIControl派生自UIView类
每个iPhone控件都有4种不同的控件状态,并且它任何时候都处于并仅能处于其中的一种状态
最常见的状态是默认的正常状态,控件在未处于其他状态时都未这种状态
UIControlState控件状态:UIControlStateNOrmal(正常,处于活动状态,但是当前并未使用), UIControlStateHighlighted(正被按住或正被使用), UIControlStateDisabled(未启用且无法使用), UIControlStateSelected(控件当前已被选中)
常用添加事件方法
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
事件通知
UIControl提供了一个标准机制,来进行事件登记和接受
可以指定控件在发生特定事件时,通知代理类的方法
要注册一个事件,可以使用addTarget方法
常用事件
UIControlEventTouchDown //单点触摸按下事件
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside //一次触摸在控件窗口内拖动
UIControlEventTouchDragOutside //一次触摸在控件窗口外拖动
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside //控件之内触摸抬起事件
UIControlEventTouchUpOutside //控件之内点击控件外抬起事件
UIControlEventTouchCancel
UIControlEventValueChanged //当控件的值发生改变时
UITextField(输入文本框)
常用属性
@property(nullable, NOnatomic,copy) NSString *text; //文本内容
@property(nullable, NOnatomic,copy) NSAttributedString *attributedText; //富文本
@property(nullable, NOnatomic,strong) UIColor *textColor; //文本颜色
@property(nullable, NOnatomic,strong) UIFont *font; //文本字体
@property(NOnatomic) NSTextAlignment textAlignment; //文本对齐方式
@property(NOnatomic) UITextBorderStyle borderStyle; //边框样式
@property(NOnatomic,readonly,getter=isEditing) BOOL editing; //只读,是否可以编辑
@property(nullable, NOnatomic,strong) UIView *leftView; //左视图
@property(NOnatomic) UITextFieldViewMode leftViewMode; //左视图模式,例如:从不出现,编辑时出现
@property(nullable, NOnatomic,strong) UIView *rightView; //右视图
@property(NOnatomic) UITextFieldViewMode rightViewMode; //右视图模式,例如:从不出现,编辑时出现
@property (nullable, readwrite, strong) UIView *inputView; //可以自定义键盘
常用UITextFieldDelegate(协议)方法
- (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是UIControl的子类,因此它具有UIControl的所有特性
常用方法
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; //设置某种状态下的标题
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state; //设置某种状态下的标题颜色
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state; //设置某种状态下的标题阴影颜色
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; //设置某种状态下的图片
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state; //设置某种状态下的背景图片
- (nullable NSString *)titleForState:(UIControlState)state; //获取某种状态下的标题
- (nullable UIColor *)titleColorForState:(UIControlState)state; //获取某种状态下的标题颜色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state; //获取某种状态下的标题阴影颜色
- (nullable UIImage *)imageForState:(UIControlState)state; //获取某种状态下的图片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state; //获取某种状态下的背景图片
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state //获取某种状态下的富文本
UISlider(滑动条)
重要知识点
UISlider是UIKit提供的一个控制条,常用于音频/视频播放的时间控制。
最常用的事件触发方式:UIControlEventValueChanged
常用属性
@property(NOnatomic) float value; //滑动条的值
@property(NOnatomic) float minimumValue; //滑动条最小值
@property(NOnatomic) float maximumValue; //滑动条最大值
@property(nullable, NOnatomic,strong) UIImage *minimumValueImage; //滑动条左侧图标
@property(nullable, NOnatomic,strong) UIImage *maximumValueImage; //滑动条右侧图标
@property(NOnatomic,getter=isContinuous) BOOL continuous; //设置是否连续地触发滑动事件
@property(nullable, NOnatomic,strong) UIColor *minimumTrackTintColor; //设置滑块左边(小于部分)线条的颜色
@property(nullable, NOnatomic,strong) UIColor *maximumTrackTintColor; //设置滑块右边(大于部分)线条的颜色
@property(nullable, NOnatomic,strong) UIColor *thumbTintColor; //设置滑块颜色(影响已划过一端的颜色)