目录
3. UITextField 文本输入框
4. UIButton 按钮
5. UITextView 多行文本
6. UIScrollView 滚动视图
3. UITextField 文本输入框
[self.view addSubview:contentTF];
[contentTF autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
//-------------- 常用 --------------
// 设置 文本
[contentTF setText:@""];
// 设置 颜色
[contentTF setTextColor:[UIColor redColor]];
// 设置 对齐方式
[contentTF setTextAlignment:NSTextAlignmentLeft];
/*
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight
*/
// 设置 字体
[contentTF setFont:[UIFont systemFontOfSize:17]];
// 设置 提示文本
[contentTF setPlaceholder:@""];
// 前提:必须先设置placeholder
// 设置 提示文本颜色
[contentTF setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
// 设置 提示文本字体
[contentTF setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
// 设置 是否加密显示
[contentTF setSecureTextEntry:true];
// 获得焦点(成为第一响应者)
[contentTF becomeFirstResponder];
// 失去焦点(注销第一响应者)
//(只能隐藏自己的键盘,不能隐藏其他textF的键盘)
[contentTF resignFirstResponder];
// 结束所有编辑状态(隐藏所有键盘)
[self.view endEditing:true];
//-------------- 右侧x视图 --------------
// 设置 右侧x视图Mode
[contentTF setClearButtonMode:UITextFieldViewModeAlways];
/*
UITextFieldViewModeNever, 从不显示
UITextFieldViewModeWhileEditing, 仅编辑时
UITextFieldViewModeUnlessEditing, 除了编辑
UITextFieldViewModeAlways 一直显示
*/
// 设置 是否插入文本时清空数据
[contentTF setClearsOnInsertion:true];
//-------------- 两侧视图 --------------
// 设置 左侧视图mode
[contentTF setLeftViewMode:UITextFieldViewModeAlways];
/*
UITextFieldViewModeNever, 从不显示
UITextFieldViewModeWhileEditing, 仅编辑时
UITextFieldViewModeUnlessEditing, 除了编辑
UITextFieldViewModeAlways 一直显示
*/
// 设置 左侧视图
[contentTF setLeftView:[UIView new]];
// 设置 右侧视图mode、右侧视图
// 右视图会覆盖x清空按钮
[contentTF setRightViewMode:UITextFieldViewModeAlways];
[contentTF setRightView:[UIView new]];
//-------------- 键盘 --------------
// 设置 键盘Mode
[contentTF setKeyboardType:UIKeyboardTypeNumberPad];
/*
UIKeyboardTypeDefault 默认的 键盘
UIKeyboardTypeASCIICapable 纯英文字母的 键盘
UIKeyboardTypeNumbersAndPunctuation 数字和标点的 键盘
UIKeyboardTypeURL 便于输入数字的 键盘 (.com)
UIKeyboardTypeNumberPad 便于输入数字的 键盘(纯数字)
UIKeyboardTypePhonePad 便于拨号呼叫的 键盘(手机号)
UIKeyboardTypeNamePhonePad 便于聊天拨号的 键盘
UIKeyboardTypeEmailAddress 便于输入Email的 键盘 (@)
UIKeyboardTypeDecimalPad 用于输入数字和【小数点】的 键盘
UIKeyboardTypeTwitter 便于在网页上书写的 键盘
UIKeyboardTypeWebSearch
UIKeyboardTypeASCIICapableNumberPad
UIKeyboardTypeAlphabet
*/
// 设置 自定义键盘View
[contentTF setInputView:[UIView new]];
// 设置 自定义键盘上方View
[contentTF setInputAccessoryView:[UIView new]];
// 设置键盘右上角return按钮标题
[contentTF setReturnKeyType:UIReturnKeyDone];
/*
UIReturnKeyDefault, 完成
UIReturnKeyGo, 完成并跳到另一页
UIReturnKeyGoogle, 搜索
UIReturnKeyJoin, 注册用户或添加数据
UIReturnKeyNext, 继续下一步
UIReturnKeyRoute, 发送
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue
*/
// 设置 键盘外观
[contentTF setKeyboardAppearance:UIKeyboardAppearanceDark];
/*
UIKeyboardAppearanceDefault, 浅灰色(默认颜色)
UIKeyboardAppearanceDark 黑色
UIKeyboardAppearanceLight 亮色,与Default很相似
UIKeyboardAppearanceAlert
*/
//-------------- rect --------------
CGRect rect=[contentTF borderRectForBounds:contentTF.bounds];
CGRect rect2=[contentTF textRectForBounds:contentTF.bounds];
CGRect rect3=[contentTF placeholderRectForBounds:contentTF.bounds];
CGRect rect4=[contentTF editingRectForBounds:contentTF.bounds];
CGRect rect5=[contentTF clearButtonRectForBounds:contentTF.bounds];
CGRect rect6=[contentTF leftViewRectForBounds:contentTF.bounds];
CGRect rect7=[contentTF rightViewRectForBounds:contentTF.bounds];
//-------------- 其他 --------------
// 设置边框Mode
[contentTF setBorderStyle:UITextBorderStyleNone];
/*
UITextBorderStyleNone, 无边框
UITextBorderStyleLine, 直线
UITextBorderStyleBezel, 边线+阴影
UITextBorderStyleRoundedRect 圆角 (默认白色背景,此时背景图无效)
*/
// 设置 背景色、背景图片
[contentTF setBackgroundColor:[UIColor blueColor]];
[contentTF setBackground:[UIImage imageNamed:@""]]; // 边框样式需为none
[contentTF setDisabledBackground:[UIImage imageNamed:@""]];
// 设置 内容纵向对齐方式
[contentTF setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
/*
UIControlContentVerticalAlignmentCenter
UIControlContentVerticalAlignmentTop
UIControlContentVerticalAlignmentBottom
UIControlContentVerticalAlignmentFill
*/
// 设置 内容横向对齐方式
[contentTF setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
/*
UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill
UIControlContentHorizontalAlignmentLeading
UIControlContentHorizontalAlignmentTrailing
*/
// 设置 是否自动补全
[contentTF setAutocorrectionType:UITextAutocorrectionTypeNo];
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo, 不自动更正
UITextAutocorrectionTypeYes, 自动更正
*/
// 设置 文本随控件缩放
[contentTF setAdjustsFontSizeToFitWidth:true];
// 设置 最小缩放字体
[contentTF setMinimumFontSize:14];
dele
//
[contentTF setDelegate:self];
// 是否允许开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{return true;}
// 开始编辑后调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{}
// 是否允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{return true;}
// 结束编辑后调用
- (void)textFieldDidEndEditing:(UITextField *)textField{}
// 是否允许改变
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// 拿到如果改变后的字符串,判断
NSString *str=[textField.text stringByReplacingCharactersInRange:range withString:string];
return true;
}
// 是否允许清除
- (BOOL)textFieldShouldClear:(UITextField *)textField{return true;}
// 是否允许return
- (BOOL)textFieldShouldReturn:(UITextField *)textField{return true;}
notification
// 由textField发出的通知:
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidEndEditingNotification
UITextFieldTextDidChangeNotification // 用于联想词汇
// 由键盘视图发出的通知:
UIKeyboardWillShowNotification 即将弹出时 发送
UIKeyboardDidShowNotification 完成弹出时 发送
UIKeyboardWillHideNotification 即将隐藏时 发送
UIKeyboardDidHideNotification 完成隐藏时 发送
UIKeyboardWillChangeFrameNotification 即将改变frame时 发送
UIKeyboardDidChangeFrameNotification 完成改变frame时 发送
IQKeyboardManager第三方(方便键盘管理)
pod 'IQKeyboardManager'
4 . UIButton 按钮
//
UIButton *button=[UIButton new];
[self.view addSubview:button];
[button autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
//-------------- 常用 --------------
// 设置 标题
[button setTitle:@"" forState:UIControlStateNormal];
/*
UIControlStateNormal 普通状态
UIControlStateHighlighted 触摸状态(高亮)
UIControlStateDisabled 禁用状态
UIControlStateSelected 选中状态
UIControlStateFocused
UIControlStateApplication
UIControlStateReserved
*/
// 设置 标题颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// 设置 字体
[button.titleLabel setFont:[UIFont systemFontOfSize:16]];
// 设置 标题阴影色
[button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
// 设置 图片
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
// 设置 图片显示模式
[button.imageView setContentMode:UIViewContentModeScaleToFill];
// 设置 标题偏移
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 图片偏移
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 内容偏移
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 背景图片
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
// 设置 背景颜色
[button setBackgroundColor:[UIColor redColor]];
// 设置 是否选中(true则进入UIControlStateSelected状态)
[button setSelected:true];
// 设置 是否允许交互
[button setUserInteractionEnabled:true];
// 添加 点击事件
[button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
//-------------- 继承自UIView --------------
// 设置 alpha透明度
[button setAlpha:0.5];
// 设置 是否隐藏
[button setHidden:true];
// 设置 填充色
[button setTintColor:[UIColor blueColor]];
// 设置是否可用
[button setEnabled:true];
// 设置 边框颜色、边框宽度、边框圆角、是否裁剪
[button.layer setBorderColor:[UIColor redColor].CGColor];
[button.layer setBorderWidth:1.0];
[button.layer setCornerRadius:5.0];
[button.layer setMasksToBounds:true];
//-------------- 其他 --------------
// 设置 不可用时是否变暗
[button setAdjustsImageWhenDisabled:true];
// 设置 触摸时是否变暗
[button setAdjustsImageWhenHighlighted:true];
// 设置 触摸时是否高亮
[button setShowsTouchWhenHighlighted:true];
// 设置 内容纵向、水平对齐方式
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
// 获取 img、bgImg、titleColor、titleShadowColor、title、AttributedTitle
UIImage *img=button.currentImage;
UIImage *bgImg=button.currentBackgroundImage;
UIColor *titleColor=button.currentTitleColor;
UIColor *shadowTitleColor=button.currentTitleShadowColor;
NSString *title=button.currentTitle;
NSAttributedString *titleStr=button.currentAttributedTitle;
SDWebImage
#import
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
系统自带类型
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
/*
UIButtonTypeCustom = 0, 前面不带图标, 默认文字颜色为白色,无触摸时高亮
UIButtonTypeSystem 前面不带图标, 默认文字颜色为蓝色,有触摸时高亮
UIButtonTypeDetailDisclosure, 前面带“!”图标按钮 ,默认文字颜色为蓝色,有触摸时高亮
UIButtonTypeInfoLight, 为感叹号“!”圆形按钮
UIButtonTypeInfoDark, 为感叹号“!”圆形按钮
UIButtonTypeContactAdd, 前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时高
UIButtonTypePlain
UIButtonTypeRoundedRect
*/
5. UITextView 多行文本
//
UITextView *contentTV=[UITextView new];
[self.view addSubview:contentTV];
// 成为第一响应者
[contentTV becomeFirstResponder];
// 注销第一响应者
[contentTV resignFirstResponder];
// 设置 frame(基本不用)
[contentTV setFrame:CGRectZero];
// 设置 文本
[contentTV setText:@""];
// 设置 颜色
[contentTV setTextColor:[UIColor redColor]];
// 设置 字体
[contentTV setFont:[UIFont systemFontOfSize:15]];
// 设置 是否允许滚动
[contentTV setScrollEnabled:true];
// 设置 文本是否可选
[contentTV setSelectable:true];
// 设置 文本是否可编辑
[contentTV setEditable:true];
// 设置 允许选择的范围
[contentTV setSelectedRange:NSMakeRange(0, 10)];
// 设置 键盘View
[contentTV setInputView:[UIView new]];
// 设置 键盘头部View
[contentTV setInputAccessoryView:[UIView new]];
// 设置 复杂标题
[contentTV setAttributedText:[NSAttributedString new]];
// 滚动至指定范围
[contentTV scrollRangeToVisible:NSMakeRange(0, 10)];
// 设置 背景色
[contentTV setBackgroundColor:[UIColor blueColor]];
// 设置 边框色、边宽宽度
[contentTV.layer setBorderColor:[UIColor blueColor].CGColor];
[contentTV.layer setBorderWidth:1.0];
// 设置 圆角
[contentTV.layer setCornerRadius:5.0];
[contentTV.layer setMasksToBounds:true];
// 判断 输入法是否是中文
BOOL isChina=[[[contentTV textInputMode] primaryLanguage]isEqualToString: @"en-US"];
// 获取 选中文本的范围
UITextRange *selectedRange = [contentTV markedTextRange];
// 获取 高亮部分?
UITextPosition *position = [contentTV positionFromPosition:selectedRange.start offset:0];
dele
// dele
[contentTV setDelegate:self];
// 开始编辑前调用(是否允许开始编辑)
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{return true;}
// 开始编辑后调用
- (void)textViewDidBeginEditing:(UITextView *)textView{}
// 结束编辑前调用(是否允许结束编辑)
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{return true;}
// 结束编辑后调用
- (void)textViewDidEndEditing:(UITextView *)textView{}
// 编辑内容后调用(是否允许编辑)用于控制字符长度
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ return true;}
// 文本内容改变后调用
- (void)textViewDidChange:(UITextView *)textView{}
// 选中文本变化后调用
- (void)textViewDidChangeSelection:(UITextView *)textView{}
// ?
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{return true;};
// ?
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{return true;};
6. UIScrollView 滚动视图
UIScrollView *contentSV=[UIScrollView new];
[self.view addSubview:contentSV];
// 设置 frame(显示区域的大小)
[contentSV setFrame:CGRectZero];
// 设置 contentSize(内容的大小(默认为CGSizeZero,必须设置,否则不滚动)(宽为0时只滚动高))
[contentSV setContentSize:CGSizeMake(100, 100)];
// 设置 偏移量(让scrollView滚动x,y)(固定的UI左上角-移动的内容左上角)
[contentSV setContentOffset:CGPointMake(100, 100)];
[contentSV setContentOffset:CGPointZero animated:true];
// 设置 内容周边(上,左,下,右)
[contentSV setContentInset:UIEdgeInsetsZero];
// 设置 弹簧效果 滚动到边界时反弹(默认true)
[contentSV setBounces:true];
// 设置 是否以页为单位(默认false)滚动
[contentSV setPagingEnabled:true];
// 设置 最小缩放比例
[contentSV setMinimumZoomScale:0.5];
// 设置 最大缩放比例
[contentSV setMaximumZoomScale:2.0];
// 设置 是否允许滚动
[contentSV setScrollEnabled:true];
// 设置 是否只允许一个方向滚动(默认false)
[contentSV setDirectionalLockEnabled:true];
// 设置 是否纵向永远可滚动
// 当sv中的子View小于sv的可视大小时默认不允许滚动,设置后允许纵向滚动(默认false )
[contentSV setAlwaysBounceVertical:true];
// 设置 是否横向永远可滚动
[contentSV setAlwaysBounceHorizontal:true];
// 设置 滚动条距sv边距
[contentSV setScrollIndicatorInsets:UIEdgeInsetsZero];
// 设置 是否允许纵向滚动
[contentSV setShowsVerticalScrollIndicator:true];
// 设置 是否允许横向滚动
[contentSV setShowsHorizontalScrollIndicator:true];
// 设置 滚动条格式
[contentSV setIndicatorStyle:UIScrollViewIndicatorStyleBlack];
/*
UIScrollViewIndicatorStyleDefault,
UIScrollViewIndicatorStyleBlack,
UIScrollViewIndicatorStyleWhite
*/
// 获取 是否开始触摸
BOOL isTracking=[contentSV isTracking];
// 获取 是否开始触摸滑动
BOOL isDragging=[contentSV isDragging];
// 获取 是否手指释放后正在滑动
BOOL isDecelerating=[contentSV isDecelerating];
// 设置 是否允许滚动到顶部(可定义一个按钮用来滚动到最上方)
[contentSV setScrollsToTop:true];
// 滚动到rect,一般用contentOffset
[contentSV scrollRectToVisible:CGRectZero animated:true];
// 设置 手指抬起后的减速率
[contentSV setDecelerationRate:UIScrollViewDecelerationRateNormal];
/*
UIScrollViewDecelerationRateNormal
UIScrollViewDecelerationRateFast
*/
// 设置 滚动时键盘的显示style,默认不消失
[contentSV setKeyboardDismissMode:UIScrollViewKeyboardDismissModeOnDrag];
/*
UIScrollViewKeyboardDismissModeNone
UIScrollViewKeyboardDismissModeOnDrag
UIScrollViewKeyboardDismissModeInteractive
*/
//
// 获取 是否正在缩放
BOOL isZooming=[contentSV isZooming];
// 获取 是否缩放超出临界值
BOOL isZoomBouncing=[contentSV isZoomBouncing];
// 获取 缩当前放比例
CGFloat zoomScale=[contentSV zoomScale];
// 缩放到某矩形块
[contentSV zoomToRect:CGRectMake(0, 0, 100, 100) animated:true];
// 缩放(动画)
[contentSV setZoomScale:0.6 animated:true];
[contentSV setZoomScale:0.6];
// ?
[contentSV setCanCancelContentTouches:true];
// ?
[contentSV setDelaysContentTouches:true];
// 设置 背景色
[contentSV setBackgroundColor:[UIColor blackColor]];
// dele
[contentSV setDelegate:self];
// 滑动时调用(持续触发)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{}
// 是否允许滚动到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{return true;}
// 滚动到顶部后调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{}
// 1.手指即将触摸前调用(此时可销毁计时器)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{}
// 2.手指离开后调用(此时可启动计时器) (decelerate:是否会继续滚动)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{}
// 3.即将减速时调用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{}
// 4.停止减速时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{}
// 5.完全停止后调用(手动滑动不调用) 设置偏移量调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{}
//
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{}
//
- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView{};
// (缩放必须+maxmin缩放值,否则不能缩放)
// 返回要缩放的对象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{}
// 缩放时调用(持续触发)
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{}
// 开始缩放前调用
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view{}
// 结束缩放后调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{}