常用UI控件和常用类

1.UILabel、UISwitch、UISlider、UIPageControl、UIActivityIndicatorView 、UIProgressView、UISegmentedControl、UITextField、UIToolbar

·UILabel
·作用:显示文本 
·常用属性

// 设置文本内容,默认为nil
@property(nonatomic,copy) NSString *text;
// 设置字体大小
@property(nonatomic,retain) UIFont *font;
// 设置字体颜色
@property(nonatomic,retain) UIColor *textColor;
// 设置阴影,默认没有阴影,如果设置需要设置偏移量 
@property(nonatomic,retain) UIColor *shadowColor;
// 设置偏移量
@property(nonatomic) CGSize shadowOffset;

// 文本内容对齐方式
@property(nonatomic) NSTextAlignment textAlignment; 
// 当文本内容超出frame时,文本截取的方式
@property(nonatomic) NSLineBreakMode lineBreakMode; 
// 文本选中时,高亮的颜色
@property(nonatomic,retain) UIColor *highlightedTextColor;
// 是否存在高亮,默认为nil 
@property(nonatomic,getter=isHighlighted) BOOL highlighted; 
// 交互是否打开,默认为NO 
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;


·UILabel示例代码

UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 0, 100, 25)];
// 背景透明
buttonLabel.backgroundColor = [UIColor clearColor];
// 设置文本颜色
buttonLabel.textColor = [UIColor whiteColor];
// 文本对齐方式
buttonLabel.textAlignment = UITextAlignmentCenter;
// 设置tag值
buttonLabel.tag = 1000;
// 设置显示文本
buttonLabel.text = @"按钮值";
// 设置文本字体
buttonLabel.font = [UIFont systemFontOfSize:14.0f];
// 文本行数
buttonLabel.numberOfLines = 0;
// Label宽度不够时,对文本进行打断的方式 
buttonLabel.lineBreakMode = UILineBreakModeCharacterWrap; 
// 根据文本自动调整Label的宽度和高度
[buttonLabel sizeToFit];


2、控件视图

·UIControl
·作用:具有处理事件处理的控件的父类 
·事件响应的3种形式:基于触摸、基于值、基于编辑



·常用方法

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents // 添加⼀一个事件
- (void)removeTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents // 移除某⼀一个事件


·事件处理

// 当用户按下时触发
UIControlEventTouchDown
// 点击计数大于1时触发 
UIControlEventTouchDownRepeat
// 当触摸在控件内拖动时触发 
UIControlEventTouchDragInside
// 当触摸在控件之外拖动时触发 
UIControlEventTouchDragOutside
// 当触摸从控件之外拖动到内部时 
UIControlEventTouchDragEnter
// 当触摸从控件内部拖动到外部时 
UIControlEventTouchDragExit
// 控件之内触摸抬起时 
UIControlEventTouchUpInside
// 控件之外触摸抬起时 
UIControlEventTouchUpOutside
// 触摸取消事件,设备被上锁或者电话呼叫打断 
UIControlEventTouchCancel

// 当控件的值发生改变时。用于滑块、分段控件等控件。
UIControlEventTouchValueChanged
// 文本控件中开始编辑时 
UIControlEventEditingDidBegin
// 文本控件中的文本被改变 
UIControlEventEditingChanged
// 文本控件中编辑结束时 
UIControlEventEditingDidEnd
// 文本控件内通过按下回车键结束编辑时 
UIControlEventEditingDidOnExit 
// 所有触摸事件 
UIControlEventAlltouchEvents
// 文本编辑的所有事件 
UIControlEventAllEditingEvents 
// 所有事件 
UIControlEventAllEvents


3、按钮控件

·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;
// 为按钮添加事件
- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents;

·UIButton的状态

UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态 
UIControlStateDisabled // 禁用状态 
UIControlStateSelected // 选中状态 
UIControlStateApplication 
UIControlStateReserved


·UIButton示例代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 40, 100, 30);
// 设置平常状态下按钮的标题
[button setTitle:@"按钮" forState:UIControlStateNormal];
// 设置高亮状态下按钮的标题
[button setTitle:@"按钮按下" forState:UIControlStateHighlighted]; 
//设置点击事件响应的方法
[button addTarget:self action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside]; 
// 设置平常状态下标题的颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置高亮状态下标题的颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
// 设置标题的字体
button.titleLabel.font = [UIFont systemFontOfSize:14];


4、文本输入控件

·UITextField视图
·作用:用户输入文字 
·常用属性

// 设置和获取文本内容,默认nil
@property(nonatomic,copy) NSString     *text;
// 设置文本内容颜色
@property(nonatomic,retain) UIColor     *textColor;
// 设置字体
@property(nonatomic,retain) UIFont     *font
// 对齐样式
@property(nonatomic) NSTextAlignment       textAlignment;
// 设置风格,默认没有风格,需要设置 
@property(nonatomic) UITextBorderStyle       borderStyle;
// 提示用户输入内容文本 
@property(nonatomic,copy) NSString     *placeholder;
// 用户编辑时是否clear内容,默认为NO 
@property(nonatomic) BOOL     clearsOnBeginEditing;
// 自适应调整字体大小,默认为NO 
@property(nonatomic) BOOL     adjustsFontSizeToFitWidth;

// 设置代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;
// 设置背景,需要将textField实例的风格设置为None
@property(nonatomic,retain) UIImage *background;
// 设置textField不可用时的背景图片
@property(nonatomic,retain) UIImage *disabledBackground; 
// 设置是否可编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
// 清除按钮的模式,默认不出现
@property(nonatomic) UITextFieldViewMode clearButtonMode;
// 自定义左视图
@property(nonatomic,retain) UIView *leftView;
// 自定义左视图出现的模式
@property(nonatomic) UITextFieldViewMode leftViewMode;
// 不用系统键盘,自定义键盘
@property (readwrite, retain) UIView *inputView;
// 系统键盘和自定义键盘共存
@property (readwrite, retain) UIView *inputAccessoryView;

//  自动大写类型
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType; 
// 检查拼写是否正确
@property(nonatomic) UITextAutocorrectionType autocorrectionType;
// 修改键盘类型
@property(nonatomic) UIKeyboardType keyboardType;
// 修改返回类型
@property(nonatomic) UIReturnKeyType returnKeyType;
// 是否安全输入,比如用户输入密码 
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;


·常用代理方法

//  将要开始输入时调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(@"开始输入");
return YES; 
}
// 将要输入结束时调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"输入结束");
return YES; 
}
// 清除文字按钮点击事件
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"清除输入内容了");
return YES; 
}
// 键盘上的return按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//隐藏输入键盘
[textField resignFirstResponder]; 
return YES;
}


·UITextField示例代码

UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 240, 200, 30)];
// 禁止首字母大写
textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; // 设置键盘类型
textfield.keyboardType = UIKeyboardTypeNamePhonePad;
// 输入框的边框类型
textfield.borderStyle = UITextBorderStyleRoundedRect;
// 设置委托代理
textfield.delegate = self;
// 键盘上的return按钮类型
textfield.returnKeyType = UIReturnKeyDone;
// 是否安全输入,是的话,输入内容将为星号
textfield.secureTextEntry = NO;
// 清除按钮模式
textfield.clearButtonMode = UITextFieldViewModeAlways;
// 输入框中的文本颜色
textfield.textColor = [UIColor redColor];
// 输入框的字体
textfield.font = [UIFont boldSystemFontOfSize:14];

5、滑动控件

·UISlider视图
·作用:控制系统声音,或者表示播放进度等等 
·常用属性

//  设置获取slider的value值
@property(nonatomic) float value;
// 设置slider的最小值
@property(nonatomic) float minimumValue;
// 设置slider的最大值
@property(nonatomic) float maximumValue;
// 设置图片
@property(nonatomic,retain) UIImage *minimumValueImage; 
// 设置图片
@property(nonatomic,retain) UIImage *maximumValueImage; 
// 设置slider的value值,是否存在动画
- (void)setValue:(float)value animated:(BOOL)animated;


·UISlider示例代码

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 0,150, 25)];
[slider addTarget:self action:@selector(sliderAction:)
            forControlEvents:UIControlEventValueChanged];
slider.maximumValue = 100;
slider.minimumValue = 0;
slider.value = 50;

6、分段控件

·UISegmentedControl
·作用:分段控件,页面的切换等等 
·示例代码

NSArray *array = [NSArray arrayWithObjects:@"选择",@"搜索",@"工具", nil]; 
UISegmentedControl *segmentCtrl = [[UISegmentedControl alloc] initWithItems:array];
segmentCtrl.frame = CGRectMake(20, 0, 150, 25); 
segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar; segmentCtrl.selectedSegmentIndex = 0;
[segmentCtrl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];

7、分页控件

·UIPageControl
·作用:通常与UIScrollView连用,提示用户当前显示的页数 
·常用属性和方法

//  共有几个分页“圆圈”
@property(nonatomic) NSInteger numberOfPages; 
// 显示当前的页
@property(nonatomic) NSInteger currentPage; 
// 只存在⼀一页时,是否隐藏,默认为YES 
@property(nonatomic) BOOL hidesForSinglePage; 
// 刷新视图
- (void)updateCurrentPageDisplay;


8、“风火轮”视图

·UIActivityIndicatorView
·作用:提示用户当前页面正在加载数据 
·常用属性和方法:

//  设置风格
@property(nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
// 停止时,隐藏视图,默认为YES
@property(nonatomic) BOOL
// 修改颜色,注意版本问题
@property (readwrite, nonatomic, retain) UIColor *color // 开始动画
- (void)startAnimating;
// 停止动画
- (void)stopAnimating;
// 判断动画的状态(停止或开始)
- (BOOL)isAnimating;

·示例代码

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(0, 0, 100, 100);
[activityView startAnimating];

9、警告视图

·UIAlertView
·作用:提示用户,帮助用户选择 
·示例代码

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:@"提示文本信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
[alertView release];

10、提示视图

·UIActionSheet
·作用:提示用户,帮助用户选择 
·示例代码

UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"other1", @"other2", nil] autorelease];
    [actionSheet showInView:self.view];

11、图片视图

·UIImageView
·作用:专用于显示图片的视图 
·常用属性和方法

//  初始化图片
- (id)initWithImage:(UIImage *)image;
// 初始化带高亮的图片
- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage
// 点语法设置图片
@property(nonatomic,retain) UIImage *image;
// 点语法设置高亮图片
@property(nonatomic,retain) UIImage *highlightedImage
// 是否打开用户交互,默认为NO @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;


·示例代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:@"t.png"];
imageView.userInteractionEnabled = YES;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值