您的位置:首页 > 产品设计 > UI/UE

IOS 常用UIKit 控件总结

2013-12-10 16:04 357 查看
在学习UIKit 控件这部分内容时,发现网上很少有做这方面总结的文章,小生通过一段时间在网上各种渠道的学习,其中部分内容为引用(对提供资料的作者致以最诚挚的谢意),对大多数常用的视图控件的功能、部分属性以及使用范例进行了总结,希望对大家有所帮助,节省更多宝贵的时间。

1. UILabel
功能: 显示文本(一般是简短的)

部分属性
@property(nonatomic , copy) NSString *text; // 设置文本内容, 默认为 nil

@property(nonatomic , retain) UIFont *font; // 设置文本字体的大小

@property(nonatomic , retain) UIColor *textColor; // 设置字体颜色

@property(nonatomic , retain) UIColor *shadowColor; // 设置阴影,默认无阴影,如果需要,则同时要设置偏移量

@property(nonatomic) CGSize shadowOffset; // 设置偏移量

@property(nonatomic) NSTextAlignment textAlignment; // 文本内容对齐方式

@property(nonatomic) NSLineBreakMode lineBreakMode; // 当文本的内容超出设定的规格后,设置文本的截取方式

@property(nonatomic , retain) UIColor *highlightedTextColor; // 文本选中时, 高亮的颜色

@property(nonatomic , getter = isHighlighted) BOOL highlighted; // 是否存在高亮, 默认为 nil

@property(nonatomic , getter = isUserInteractionEnabled) BOOL userInteractionEnabled; // 交互是否打开, 默认为 NO

UILabel 实例:

UILabel *label = [[UILabel alloc ] initWithFrame: CGRectMake(50, 60, 230, 90)]; label.backgroundColor = [UIColor clearColor]; // 背景色为透明色

label.textColor = [UIColor cyanColor]; // 文本颜色

label.textAlignment = UITextAlignmentCenter; // 文本对齐方式

label.tag = 1; // 设置该实例对象的标示值

label.text = @" Happy New Year "; // 设置文本内容

label.font = [UIFont systemFontOfSize:16.0f]; // 设置文本字体

label.numberOfLines = 12; // 设置文本的行数

label.lineBreakMode = UILineBreakModeCharacterWrap; // 文本一行的内容超过预定的行宽时,对文本的打断方式

[label sizeToFit]; // 根据文本大小,自动调整 label 的宽度和高度

2.UIControl

功能:是一些具有 事件处理 的控件的父类

时间相应地3种方式:基于触摸、基于值、基于编辑。

常用方法:

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

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

事件处理

UIControlEventTouchDown // 用户按下时触发 事件

UIControlEventTouchDownRepeat // 双击触发

UIControlEventTouchDragInside // 在控件内拖动时触发

UIControlEventTouchDragOutside // 在控件之外拖动时触发

UIControlEventTouchDragEnter // 从控件外拖动到控件里触发

UIControlEventTouchDragExit // 从控件内部拖动到控件外部

UIControlEventTouchUpInside // 触摸控件内部结束时,触发 事件

UIControlEventTouchUpOutside // 触摸控件外部结束时, 触发 事件

UIControlEventTouchCancel // 触摸取消事件

UIControlEventTouchValueChanged // 当控件的值发生改变时,用于 UISlider、UISegment

UIControlEventTouchEditingDidBegin // 文本控件开始编辑时

UIControlEventTouchEditingChanged // 文本控件中的文本被改变时

UIControlEventTouchEditingDidEnd // 文本控件结束编辑时

UIControlEventTouchEditingDidOnExit // 文本控件内通过按下回车键结束的时候

UIControlEventAllTouchEvents // 所有触摸事件

UIControlEventAllEditingEvents // 文本编辑的所有事件

UIControlEventAllEvents // 所有事件

3. UIButton

功能:用于响应用户点击事件的界面元素

部分属性

-(void) setTitle:(NSString*)title forState:(UIControlState)state; // 设置指定状态对应的标题文本

-(void) seTitleColor:(UIColor*)color forState:(UIControlState)state; // 设置指定状态对应的标题颜色

-(void) setImage:(UIImage*)image forState:(UIControlState)state; // 设置指定状态对应的显示图片

-(void) setBackgroundImage:(UIImage*)image forState:(UIControState)state; // 设置指定状态对应的背景图片

-(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 添加按钮对应的某一个事件

UIButton的state(状态)

UIControlStateNormal // 正常

UIControlStateHighlighted // 高亮

UIControlStateDisabled // 禁用

UIControlStateSelected // 选中

UIControlStateApplication // 应用程序标志时

UIControlStateReserved // 内部框架预留时

UIButton 实例:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

/* 可以定义的 button类型有以下6种:

typedef enum{

UIButtonTypeCustom = 0, 自定义

UIButtonTypeRoundedRect, 矩形边角为圆角

UIButtonTypeDetailDisclosure,蓝色小箭头按钮, 通常作详细说明用

UIButtonTypeInfoLight, 亮色感叹号

UIButtonTypeInfoDark, 暗色感叹号

UIButtonTypeContactAdd, 十字加号按钮

} UIButtonType;

*/

button.frame = CGRectMake(10, 40, 100, 100); // 设定button 的坐标和规格

button.backgroundColor = [UIColor clearColor];

[button setTitle:@"Click" forState:UIControlStateNormal];

/* forState: 这个参数的作用是定义按钮的文字或图片,在何种状态下才会显现

有以下几种状态:

enum{

UIControlStateNormal = 0, 常规状态显现

UIControlStateHighlighted = 1 << 0 , 高亮状态显示

UIControlStateDisabled = 1 << 1 , 禁用状态才会显示

UIControlStateSelected = 1 << 2 , 选中状态

UIControlStateApplication = 0x00ff0000, 当应用程序标志时

UIControlStateReserved = 0xff000000, 为内部框架预留

};*/

/* 默认情况下, 当按钮高亮的情况下,图像的颜色会深一些, 如果下面的这个属性设为NO,则可以禁用该功能

*/

button.adjustsImageWhenHighlighted = NO;

/* 与上面的情况相同,当按钮禁用的情况下 ,,,*/

button.adjustsImageWhenDisabled = NO;

/* 设定以下属性为YES,使按钮被按下时会发光 */

button.showsTouchWhenHighlighted = YES;

/* 按下按钮,当手指离开屏幕时,触发这个事件。触发事件后,执行 buttonClick: 方法, addTarget: self 表明,这个方法在本类中也可以传入其它类的指针 */

[button addTarget: self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubView: button]; // 显示控件

4. UITextField

功能:让用户输入文字信息

部分属性

@property(nonatomic , copy) NSString *text; // 设置或获取文本内容,默认为nil

@property(nonatomic , retain) UIColor *textColor; // 设置文本内容颜色

@property(nonatomic , retain) UIFont *font; // 设置字体

@property(nonatomic) NSTextAlignment textAlignment; // 对齐样式

@property(nonatomic) UITextBorderStyle borderStyle; // 设置风格,默认无风格,需要设置

@property(nonatomic , copy) NSString *placeholder; // 提示用户输入内容文本

@property(nonatomic) BOOL clearsOnBeginEditing; // 用户编辑时,时候clear内容,默认为NO

@property(nonatomic) BOOL adjustsFontSizeToFitWidth; // 自动适应调整字体的大小,默认为NO

@property(nonatomic , assign) id<UITextFieldDelegate> delegate; // 设置代理

@property(nonatomic , retain) UIImage *background; // 设置背景,需要将textField实例的风格设置为None

@property(nonatomic , retain) UIImage *disableBackground; // 设置textField 不可用时的背景图片

@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(@"start inputting");

return YES;

}

// 将要输入结束是调用

-(BOOL) textFieldShouldEndEditing:(UITextField*) textField{

NSLog(@" The end of inputting");

return YES;

}

// 清除文字按钮点击事件

-(BOOL) textFieldShouldClear:(UITextField*) textField{

NSLog(@"The content has been removed.");

return YES;

}

// 键盘上的return 按钮

-(BOOL) textFieldShouldReturn:(UITextField*) textField{

[textField resignFirstResponder]; // 隐藏输入键盘

return YES;

}

UITextField实例:

UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, 320, 240)];

textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; // 禁止首字母大写

textfield.keyboardType = UIKeyboardTypeNamePhonePad; // 设置键盘类型

textfield.borderStyle = UITextBorderStyleRoundedRect; // 输入框的边框类型

textfield.delegate = self; // 设置委托代理

textfield.returnKeyType = UIReturnKeyDone; // 键盘上的return按钮类型

textfield.secureTextEntry = NO; // 是否安全输入,若设置为YES,则显示的输入内容为星号

textfield.clearButtonMode = UITextFieldViewModeAlways; // 清除按钮模式

textfield.textColor = [UIColor redColor]; // 输入框中的文本颜色

textfield.font = [UIFont boldSystemFontOfSize: 14]; // 输入框的字体

5. UISlider

功能:控制声音大小,或是表示播放进度等

部分属性

@property(nonatomic) float value; // 设置获取slider 的 value 值

@property(nonatomic) float minimumValue; // 设置slider 的最小值

@property(nonatomic) float maximumValue; // 设置slider 的最大值

@property(nonatomic , retain) UIImage *minimumValueImage; // 设置图片数量的最小值

@property(nonatomic , retain) UIImage *maximumValueImage; // 设置图片数量的最大值

-(void) setValue:(float)value animated:(BOOL)animated; // 设置slider 的value值,是否存在动画

UISlider实例:

UISlider *slider = [[UISlider alloc] initWithFrame: CGRectMake(20, 20, 100, 100)];

[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

slider.maximumValue = 100;

slider.minimumValue = 0;

slider.value = 50;

6. UISegmentedControl

功能:可用于页面的切换,

UISegmentedControl 实例:

NSArray *array = [NSArray arrayWithObjects:@"Tom",@"Jim",@"jack",nil];

UISegmentedControl *segmentCtrl = [UISegmentedControl alloc] initWithItems: array];

segmentCtrl.frame = CGRectMake(0, 0, 230, 140);

segmentCtrl.segmentedControlStyle = UISegmentedControlStyleBar;

segmentCtrl.selectedSegmentIndex = 0;

[segmentCtrl addTarget: self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

7. UIPageControl

功能:通常和UIScrolView连用, 提示用户当前显示的页数

属性及方法

@property(nonatomic) NSInteger numberOfPages; // 分页的“小圈”数

@property(nonatomic) NSInteger currentPage; // 显示当前的页

@property(nonatomic) BOOL hidesForSinglePage; // 只存在一页时,是否隐藏“小圈”, 默认为YES

-(void) updateCurrentPageDisplay; // 刷新视图

8. UIActivityIndicatorView

功能:提示用户当前页正在加载数据

部分属性和方法

@property(nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle;

// 设置风格

@property(nonatomic) BOOL hidesWhenStopped;// 停止时,隐藏该加载提示视图,默认为YES

@property(readwrite, nonatomic, retain) UIColor *color; // 修改该视图的颜色

-(void) startAnimating; // 开始动画

-(void) stopAnimating; // 停止动画

-(BOOL) isAnimating; // 判断动画的状态

UIActivityIndicatorViewStyle 实例:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];

activityView.frame = CGRectMake(20, 20, 40, 60);

[activityView startAnimating];

9. UIAlertView

功能:显示必要的提示信息,在某些时刻给出警示

UIAlertView 实例:

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle: @"标题"

message: @"提示的文本信息"

delegate: self

cancelButtonTitle: @"取消"

otherButtonTitles: @"确定", nil];

[alertView show];

[alertView release];

10. UIImageView

功能:专门用于显示图片的视图

部分属性和方法

-(id) initWithImage:(UIImage*)image; // 初始化图片

-(id) initWithImage:(UIImage*)image highlightedImage:(UIImage*)highlightedImage;

// 初始化带高亮的图片

@property(nonatomic , retain) UIImage *image; // 点语法设置图片

@property(nonatomic , retain) UIImage *highlightedImage; // 点语法设置高亮图片

@property(nonatomic , getter=isUserInteractionEnabled) BOOL userInteractionEnabled;

// 是否打开用户交互, 默认为NO

UIImageView 实例:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,60)];

imageView.image = [UIImage imageNamed:@"photo.png"];

imageView.userInteractionEnabled = YES;

如有错误,还望指出,十分感谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: