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;