UIButton(按钮)
初始化方法
- (id)initWithFrame:(CGRect)frame;
+ (id)buttonWithType:(UIButtonType)buttonType;
按钮样式(UIButtonType)
- UIButtonTypeCustom:自定义样式
- UIButtonTypeSystem:系统样式
显示状态(UIControlState)
- UIControlStateNormal:普通状态
- UIControlStateHighlighted:高亮状态
- UIControlStateSelected:选中状态
常用属性
- showsTouchWhenHighlighted:点击按钮是否高亮
- tintColor:前景色(模板色)
- titleLabel:按钮系统封装的标题文本
- imageView:按钮系统封装的图片视图
- selected:选中状态
常用方法
- 设置标题与标题颜色
// 设置标题
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
// 设置标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- 设置图片与背景图片
// 设置背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
// 设置图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- 设置标题属性
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state
- 事件添加
/**
* 添加按钮事件
*
* @param target 执行对象
* @param action 执行方法
* @param controlEvents 触发行为
*/
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
UIControl事件类型
- UIControl继承于UIView,是一些可交互控件的父类,如UIButton、UISwitch、UITextField、UISlider等
- UIControl维护着一些常规的事件类型,来指定在何种事件发生时响应方法值改变
- UIControlEvents(触发按钮行为)
UIControlEventTouchDown:触摸按下
UIControlEventTouchCancel:触摸取消
UIControlEventValueChanged:控件值改变
UIControlEventTouchUpInside:触摸按下并抬起
UIControlEventTouchUpOutside: 触摸按下并在边界外抬起
UIControlEventTouchDownRepeat:触摸按下多次
UITextField(文本输入框)
初始化方法
- (id)initWithFrame:(CGRect)frame;
常用属性
- text:文本输入框文本
- font:字体
- placeholder:占位符
- tintColor:前景色(该属性可修改光标颜色)
- backgroundColor:背景色
- textColor:字体颜色
- clearButtonMode:清除模式
- keyboardType:键盘样式
- borderStyle:边框样式
- delegate:代理
- secureTextEntry:是否安全输入
- textAlignment:文本对齐方式
- autocorrectionType:是否纠错(联想)
- keyboardAppearance:键盘样式
- adjustsFontSizeToFitWidth:是否自动调整字体大小
- contentVerticalAlignment:垂直对齐方式
- clearsOnBeginEditing:是否再次编辑清空
- autocapitalizationType:是否首字母大写
- returnKeyType:return按钮样式
- minimumFontSize:自动缩小显示的最小字体
- inputAccessoryView:输入附属视图(可自定义)
- inputView:输入视图即键盘视图(可自定义)
常用方法
收起键盘方法
// 方法1:文本输入框失去第一响应
[文本输入框实例化对象 resignFirstResponder]
// 方法2:停止界面编辑
[self.view endEditing:YES];
自定义键盘样式
inputView:输入视图
inputAccessoryView:输入附属视图
以上两种视图可自定义,定义方法有两种,一种是直接通过属性赋值的形式;另一种则需要子类化UITextField
,并重写其对应的getter()
方法即可,如下将主要展示如何通过子类化的形式自定义键盘样式。
1、首先第一步,创建一个类CustomTextField
继承于UITextField
。
2、在CustomTextField.m
文件中,重写如下方法:
2.1、自定义键盘附属视图
- (UIView *)inputAccessoryView {
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
view.backgroundColor = [UIColor blackColor];
UILabel * textLabel = [[UILabel alloc]initWithFrame:view.frame];
textLabel.text = @"自定义键盘附属视图";
textLabel.textColor = [UIColor whiteColor];
textLabel.textAlignment = NSTextAlignmentCenter;
[view addSubview:textLabel];
return view;
}
2.2、自定义键盘视图
- (UIView *)inputView{
UIView * keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 220)];
keyBoardView.backgroundColor = [UIColor greenColor];
UILabel * textLabel = [[UILabel alloc]initWithFrame:keyBoardView.frame];
textLabel.text = @"自定义键盘";
textLabel.textAlignment = NSTextAlignmentCenter;
[keyBoardView addSubview:textLabel];
return keyBoardView;
}
3、效果展示
键盘通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
- UIKeyboardWillShowNotification
- UIKeyboardDidShowNotification
- UIKeyboardWillHideNotification
- UIKeyboardDidHideNotification
UITextFieldDelegate(协议方法)
// 1、是否允许开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
// 2、开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField;
// 3、是否允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
// 4、结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField;
// 5、是否允许改变文本输入框字符长度:可在此方法中限制用户输入字符长度、是否输入敏感字符等;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
// 6、是否允许清除
- (BOOL)textFieldShouldClear:(UITextField *)textField;
// 7、是否相应return键:用户点击键盘上的return键会触发此方法;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
UILabel(文本标签)
初始化方法
- (instancetype)initWithFrame:(CGRect)frame;
常用属性
- text:文本
- font:字体
- textColor:字体颜色
- shadowColor:阴影颜色
- shadowOffset:阴影偏移
- textAlignment:文本对齐方式
- lineBreakMode:文本换行方式
- numberOfLines:行数
- userInteractionEnabled:是否允许用户交互
- adjustsFontSizeToFitWidth:是否调整字体大小适应文本标签宽度
常用方法
- 文本自适应方法1:
[文本标签对象 sizeToFit];
文本自适应方法2:
steps 1:自定义方法获取文本自适应后的宽度和高度(CGSize)
/**
* 文本自适应
*
* @param string 字符串
* @param font 字体
* @param constrainSize 文本约束
*
* @return 返回自适应后的大小
*/
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font constainSize:(CGSize)constrainSize
{
CGSize size;
CGRect rect = [string boundingRectWithSize:constrainSize // 显示的最大容量
options:NSStringDrawingUsesLineFragmentOrigin| // 附加参数
NSStringDrawingUsesFontLeading|
NSStringDrawingTruncatesLastVisibleLine
attributes:@{NSFontAttributeName:font} // 描述字符串的属性字典
context:nil]; // 上下文
size = rect.size;
return size;
}
steps 2:调用自定义方法获取size
CGSize size = [self sizeWithString:context font:[UIFont systemFontOfSize:17] constainSize:CGSizeMake(150, 1000)];
steps 3:根据返回size初始化文本标签
UILabel *label = [UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height);