UI整理-----part2--UI控件
1.label
(1)label的默认行数是1,可以通过label.numberOfLines = 0 实现自动换行
(2)通过 [label sizeToFit] 可以让label根据text适当设置高度和宽度
2.button
(1)可以通过 UIButton *but = [UIButtonbuttonWithType:<#(UIButtonType)#>] ; 设置button的类型
typedefNS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // nobuttontype 自定义风格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standardsystembutton
UIButtonTypeDetailDisclosure, 蓝色小箭头
UIButtonTypeInfoLight, 亮色感叹号
UIButtonTypeInfoDark, 暗色感叹号
UIButtonTypeContactAdd, 十字加号按钮
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, useUIButtonTypeSysteminstead 圆角矩形
};
(2)UIControlState
typedefNS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0, 常规
UIControlStateHighlighted = 1 << 0, // usedwhenUIControlisHighlightedisset 高亮
UIControlStateDisabled = 1 << 1, 禁用状态
UIControlStateSelected = 1 << 2, // flagusablebyapp (seebelow) 选中
UIControlStateFocusedNS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicableonlywhenthescreensupportsfocus
UIControlStateApplication = 0x00FF0000, // additionalflagsavailableforapplicationuse 当应用程序标志时
UIControlStateReserved = 0xFF000000 // flagsreservedforinternalframeworkuse为内部框架预留
};
(3)@property(nonatomic)BOOL adjustsImageWhenHighlighted; 高亮状态下是否会改变图片状态
@property(nonatomic)BOOLshowsTouchWhenHighlighted__TVOS_PROHIBITED; 按下按钮会发亮
@property(nonatomic)BOOLadjustsImageWhenDisabled; 按钮禁用时图像是否会变深
(4)Button事件
typedefNS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // onalltouchdowns 单点触摸按下事件
UIControlEventTouchDownRepeat = 1 << 1, // onmultipletouchdowns (tapcount > 1) 多点触摸按下事件
UIControlEventTouchDragInside = 1 << 2, 当一次触摸在控件窗口内拖动
UIControlEventTouchDragOutside = 1 << 3, 当一次触摸在控件窗口外拖动
UIControlEventTouchDragEnter = 1 << 4, 当一次触摸在控件窗口外拖入
UIControlEventTouchDragExit = 1 << 5, 当一次触摸在控件窗口内拖出
UIControlEventTouchUpInside = 1 << 6, 所有在控件之内触摸抬起事件
UIControlEventTouchUpOutside = 1 << 7, 所有在控件之外触摸抬起事件
UIControlEventTouchCancel = 1 << 8, 所有触摸取消事件
UIControlEventValueChanged = 1 << 12, // sliders, etc. 控件的值发生改变时
UIControlEventPrimaryActionTriggeredNS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semanticaction: forbuttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField 当文本控件中开始编辑发送通知
UIControlEventEditingChanged = 1 << 17, 当文本控件中文本变化时发送通知
UIControlEventEditingDidEnd = 1 << 18, 当文本控件中文本编辑结束时发送通知
UIControlEventEditingDidEndOnExit = 1 << 19, // 'returnkey' endingediting 当文本控件中通过按下回车键或等价行为结束编辑时
UIControlEventAllTouchEvents = 0x00000FFF, // fortouchevents 通知所有触摸事件
UIControlEventAllEditingEvents = 0x000F0000, // forUITextField 通知所有关于文本编辑的事件
UIControlEventApplicationReserved = 0x0F000000, // rangeavailableforapplicationuse
UIControlEventSystemReserved = 0xF0000000, // rangereservedforinternalframeworkuse
UIControlEventAllEvents = 0xFFFFFFFF 通知所有事件
};
3.UITextFiled
(1)UITextfield可以实现文本的编辑,比label多了一个键盘的使用,此外还要签一个协议 <UITextFieldDelegate>
(2)设置 textfield.borderStyle 可以实现设置边框类型
typedefNS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone, 无边框
UITextBorderStyleLine, 有边框
UITextBorderStyleBezel, 有边框和阴影
UITextBorderStyleRoundedRect 边框有圆角
};
(3)@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry; 安全输入设置 默认为no
(4)@property(nullable, nonatomic,copy)NSString *placeholder; 提示文字的设置
(5)设置键盘类型
typedefNS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // Defaulttypeforthecurrentinputmethod. 默认键盘 支持所有类型
UIKeyboardTypeASCIICapable, // DisplaysakeyboardwhichcanenterASCIIcharacters, non-ASCIIkeyboardsremainactive 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, // Numbersandassortedpunctuation. 标准电话键盘
UIKeyboardTypeURL, // AtypeoptimizedforURLentry (shows . / .comprominently). URL键盘,支持.com按钮,只支持URL字符
UIKeyboardTypeNumberPad, // Anumberpad (0-9). SuitableforPINentry. 数字键盘
UIKeyboardTypePhonePad, // Aphonepad (1-9, *, 0, #, withlettersunderthenumbers). 电话键盘
UIKeyboardTypeNamePhonePad, // Atypeoptimizedforenteringaperson'snameorphonenumber. 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, // Atypeoptimizedformultipleemailaddressentry (showsspace@ . prominently). 用于输入电子邮箱的键盘
UIKeyboardTypeDecimalPadNS_ENUM_AVAILABLE_IOS(4_1), // Anumberpadwithadecimalpoint. 数字键盘,有数字和小数点
UIKeyboardTypeTwitterNS_ENUM_AVAILABLE_IOS(5_0), // Atypeoptimizedfortwittertextentry (easyaccessto@ #) 优化的键盘 方便输入@
UIKeyboardTypeWebSearchNS_ENUM_AVAILABLE_IOS(7_0), // AdefaultkeyboardtypewithURL-orientedaddition (showsspace . prominently).
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
};
(6)设置return样式
typedefNS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinueNS_ENUM_AVAILABLE_IOS(9_0),
};
(7)可以通过 [[UITextField appearance] setKeyboardAppearance:] 设置键盘风格
typedefNS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, // Defaultapperanceforthecurrentinputmethod. 默认外观 浅灰色
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated 深灰 石墨色
};
(8)可以通过 @property(nonatomic)UITextFieldViewMode clearButtonMode; 设置清除按钮
typedefNS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
(9)设置弹出视图(不弹出键盘弹出图片)
UITextField *textfield;
UIImageView *imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:(nonnullNSString *)]];
imageView.frame = CGRectMake(0, 100, 320, 100); //跟位置和宽无关
textfield.inputView = imageView;
(10)设置右视图
UITextField *textfield;
UIView *rightView;
textfield.rightView = rightView;
textfield.rightViewMode = UITextFieldViewModeAlways;//会影响清除按钮模式
(11)通过 textfield.clearsOnBeginEditing 可以选择在再次编辑时是否清空之前的内容
(12)可以通过以下代码调节编辑内容时垂直对齐方式:textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//默认是top显示对齐
(13)设置滚动
textfield.font = [UIFontsystemFontOfSize:30]; // 可以设置字体大小
textfield.adjustsFontSizeToFitWidth = YES; //默认是no,yes:当充满边框时,文字会缩小,当小到一定程度时仍然会滚动,自适应宽度
textfield.minimumFontSize = 20; // 设置滚动时的最小字号
(14)首字母是否大写 :textfield.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;//实现所有字母大写
typedefNS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子首字母大写
UITextAutocapitalizationTypeAllCharacters,所有字母大写
};
(15)[textfieldresignFirstResponder];//取消当前textfield第一响应者身份
(16)点击return时调用的方法:- (BOOL)textFieldShouldReturn:(UITextField *)textField{}
(17)限制只能输入特定字符
-(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string
{
NSCharacterSet *cs;
cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBER] invertedSet]; //#defineNUMBER@"012345678\n" \n是为了触发done
NSString *filtered = [[stringcomponentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL canChange = [stringisEqualToString:filtered];
return canChange;
}
如果限制英文和数字 #define kALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
(18)限制只输入一定长度的字符串
- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string
{
//String就是此刻输入的字符textfield是正在输入的输入框,返回YES是可以改变输入框的值NO相反
if ([stringisEqualToString:@"\n"]) { //按回车可改变
return YES;
}
NSString *toBeString = [textField.textstringByReplacingCharactersInRange:rangewithString:string]; //得到输入框的内容
if (self.myTextField == textField) { //判断是否是我们想要想定输入框
if ([toBeStringlength] > 20) { //如果输入框内容大于20弹出框
textField.text = [toBeStringsubstringToIndex:20];
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:nilmessage:@"超过最大字数不能输入了"delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil] ;
[alertshow];
returnNO;
}
}
returnYES;
}
4.UITextView
5.UIImageView
(1)@property (nullable, nonatomic, strong) UIImage *highlightedImageNS_AVAILABLE_IOS(3_0); // default is nil 高亮状态下的图片,默认为nil
(2)@property(nonatomic) NSInteger tag;目的是给个标记方便查找
(3)@property(nonatomic,readonly,strong) CALayer *layer; 代表一个layer层,通过设置layer可以实现:
<1>设置边框线宽度 @property CGFloat borderWidth;
<2>设置边框颜色 @property(nullable) CGColorRef borderColor imageView.layer.borderColor = [UIColor cyanColor].CGColor;
<3>设置圆角 @property CGFloat cornerRadius;
<4>裁掉超出layer层的视图(默认为NO)
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;//设置图片的显示方式
6.设置动画
(1)通过图片的累加完成动画:首先创建一个可变数组接收图片,然后将数组设为动画对象,再通过相应按钮控制动画
.h中
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
.m中
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:1];
for (int i = 1; i < 21; i ++) {
NSString *name = [NSString stringWithFormat:@"%d.png",i];
UIImage *image = [UIImage imageNamed:name];
[array addObject:image];
}
_imageView.animationImages = array;
_imageView.animationDuration = 2;
_imageView.animationRepeatCount = HUGE_VALF;
- (IBAction)startAction:(UIButton *)sender {
[_imageViewstartAnimating];
}
- (IBAction)stopAction:(UIButton *)sender {
[_imageViewstopAnimating];
}
7.UISwitch
(1)可以通过其valueChange来检测开关切换,并通过属性on或实例方法isOn来获取当前值
8.UISlider
(1)@property(nonatomic) float value; // default 0.0. thisvaluewillbepinnedtomin/max 可以设置滑块位置,如果没有设置边界值默认为 0 - 1 。
(2)@property(nonatomic) floatminimumValue; // default 0.0. the current value may change if outside new min value
@property(nonatomic) floatmaximumValue; // default 1.0. the current value may change if out side new max value
(3)@property(nullable, nonatomic,strong) UIImage *minimumValueImage; // default is nil. image that appears to left of control (e.g. speakeroff) 即最小值端图片
@property(nullable, nonatomic,strong) UIImage *maximumValueImage; // default is nil. image that appears to right of control (e.g. speakermax) 即最大值端图片
(4)是否连续变化(默认是YES): @property(nonatomic,getter = isContinuous) BOOL continuous; // if set, value change events are generated any time the value changes due to dragging. default = YES
(5)设置滑块两侧的颜色:@property(nullable, nonatomic,strong) UIColor *minimumTrackTintColor;
@property(nullable, nonatomic,strong) UIColor *maximumTrackTintColor;
(6)设置已滑过一端的颜色:@property(nullable, nonatomic,strong) UIColor *thumbTintColor;
(7)设置滑块的图片:- (void)setThumbImage:(nullableUIImage *)imageforState:(UIControlState)state;
(8)设置滑块滑过部分的线条图案:- (void)setMinimumTrackImage:(nullableUIImage *)imageforState:(UIControlState)state;
(9)设置滑块未滑过部分的线条图案:- (void)setMaximumTrackImage:(nullableUIImage *)imageforState:(UIControlState)state;
(10)设置当前状态的响应属性的方法:@property(nullable,nonatomic,readonly) UIImage *currentThumbImage;
@property(nullable,nonatomic,readonly) UIImage *currentMinimumTrackImage;
@property(nullable,nonatomic,readonly) UIImage *currentMaximumTrackImage;
(11)添加触发事件:- (void)addTarget:(nullableid)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents;
9.UISegementControl
(1)创建:NSArray *array = @[@"好友",@"备胎",@"男神",@"黑名单"];
UISegmentedControl *segmentC = [[UISegmentedControlalloc]initWithItems:array] ;
segmentC.frame = CGRectMake(50, 250, 250, 50);
(2)设置默认分组:segmentC.selectedSegmentIndex = 1;//从0开始
(3)设置颜色:segmentC.tintColor = [UIColororangeColor];
(4)实现点击方法:- (void)addTarget:(nullableid)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents;
10.UIPageControl
(1)通过 @property(nonatomic) NSIntegernumberOfPages; // defaultis 0 实现设置页数
(2)通过@property(nonatomic) NSIntegercurrentPage; // defaultis 0. valuepinnedto 0..numberOfPages-1 设置当前页
(3)通过pageC.backgroundColor 可以设置CGMake定义的范围的颜色;通过pageC.currentPageIndicatorTintColor 可以设置选中的颜色;通过pageC.pageIndicatorTintColor也可以把其它点设置上颜色
11.UIProgressView
(1)通过 - (instancetype)initWithProgressViewStyle:(UIProgressViewStyle)style; // setstheviewheightaccordingtothestyle 方法可以给初始化的进度条一个长度,进度条的长度可以通过frame来设置,但是只有三个参数有效。
typedefNS_ENUM(NSInteger, UIProgressViewStyle) {
UIProgressViewStyleDefault, // normal progress bar 普通样式
UIProgressViewStyleBar__TVOS_PROHIBITED, // for use in a tool bar 用于工具条的样式
};
(2)设置进度条:
progressV.progressViewStyle; 风格样式
progressV.progress; 进度条进度
progressV.progressTintColor; 已走过进度条颜色
progressV.progressImage; 未走过进度条颜色
progressV.trackImage; 未走过进度背景图
(3)设置进度条并设置是否动画显示(动画显示会平滑过渡):- (void)setProgress:(float)progressanimated:(BOOL)animatedNS_AVAILABLE_IOS(5_0);
12.UIActivityIndicatorView 风火轮
UIActivityIndicatorView *acitivity;
[acitivity startAnimating];//开启风火轮默认状态不显示
[acitivity stopAnimating]; //停止风火轮
13.UIAlertController
(1)UIAlertController 通过 + (instancetype)alertControllerWithTitle:(nullableNSString *)title message:(nullableNSString *)messagepreferredStyle:(UIAlertControllerStyle)preferredStyle 可以创建出alertController (Title是题目 message是小标题);
(2)UIAlertAction 通过 + (instancetype)actionWithTitle:(NSString *)title style:(UIPreviewActionStyle)style handler:(void (^)(UIPreviewAction *action, UIViewController *previewViewController))handler; 创建事件,其中的 style有三种:
typedefNS_ENUM(NSInteger,UIPreviewActionStyle) {
UIPreviewActionStyleDefault=0, 默认的
UIPreviewActionStyleSelected, 作用是取消
UIPreviewActionStyleDestructive, 背景是红色
} ;
(3)通过设置 alertC.preferredStyle 可以实现为
typedefNS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0, 操作表风格
UIAlertControllerStyleAlert 警告框风格
};
(4)只有在alert下才可以用textfield,alertController通过 - (void)addTextFieldWithConfigurationHandler:(void (^__nullable)(UITextField *textField))configurationHandler; 方法可以添加textfield用于输入