iOS開發之iOS界面UI

一、UILabel緩存

 1 NSString *str = @"字符串大小";
 2 UIFont *font = [UIFont fontWithName:@"Arial" size:50.0f];
 3 CGSize size = CGSizeMake(320, 2000);
 4 UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
 5 [label setNumberOfLines:0];
 6 CGSize labelsize = [str sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeCharacterWrap];
 7 label.frame = CGRectMake(0, 0, labelsize.width, labelsize.height);
 8 label.textColor = [UIColor blackColor];
 9 label.text = str;
10 label.font = font;
11 [self.view addSubview:label];

UILabel主要屬性:ide

  text:設置UILabel的文本內容,NSString類型;函數

  font:設置文本的字體,UIFont類型;字體

  textColor:設置文本的顏色,UIColor類型;ui

  lineBreakMode:設置折行的模式,UILineBreakMode類型,通常爲UILineBreakModeWordWrap;atom

  textAlignment:設置文本的對齊方式,UITextAlignment,有左、中、右spa

 

二、UIButton代理

繼承於UIControl基類code

UIButton缺省是圓角按鈕,還有圖片按鈕、Info light、Info dark、Contack add、Detail disclosurecomponent

 

Button的點擊事件中獲取點擊Button對象

1 - (IBAction) buttonClick:(id)sender
2 {
3     //將sender強制轉換成Button類型,獲取哪一個按鈕觸發的點擊事件
4     UIButton *button = (UIButton *)sender;
5     .
6     .
7     .
8 }

- (void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents:

  給按鈕增長一個按鈕事件的處理函數

- (void) removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents:

  刪除按鈕的事件處理函數

手動建立Button

  手動建立Button,而且增長按鈕點擊事件

  UIControlEventTouchUpInside就是當手指點擊按鈕後離開屏幕的事件

1 UIButton *button  = [UIButton buttonWithType:UIButtonTypeRoundedRect];
2 button.frame = CGRectMake(150, 25, 72, 37);
3 [button setTitle:@"按鈕" forState:UIControlStateNormal];
4 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
5 button.tag = 6;

tag是惟一識別每一個按鈕的屬性

IBAction實際上就是一個void類型,只不過IBAction可讓Interface Builder知道這是一個按鈕的點擊事件。

 

三、NSDatePicker

一、NSDate類:是系統一個日期、時間類。

+(id)date:返回當前的日期、時間;

+(id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs:返回將來secs秒後的日期、時間;

+(id)distantFuture:將來永遠達不到的時間;

+(id)distantPast:過去的時間。

二、NSDateFormatter

1 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY-MM-dd"]; 
2 NSString *d1 = [dateFormatter stringFromDate:date]; 
3 NSLog(@"date is %@", d1);  //輸出2013-10-26
4 
5 [dateFormatter setDateFormat:@"YYYY年MM月dd日"]; 
6 NSString *d2 = [dateFormatter stringFromDate:date]; 
7 NSLog(@"date is %@", d2);  //輸出2011年11月24日

三、NSCalendar:獲得當前日期、時間

1 NSDate *date = [NSDate date];
2 NSCalendar *cal = [NSCalendar currentCalendar];
3 NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:date];

設置當前日期、時間:
-(void) setYear:(NSInteger)v;

-(void) setMonth:(NSInteger)v;

-(void) setDay:(NSInteger)v;

-(void)setHour:(NSInteger)v;

-(void)setMinute(NSInteger)v;

-(void)setSecond:(NSInteger)v;

四、UIDatePicker

UIDatePicker事件處理:

UIControlEventValueChanged:UIDatePicker每次值改變時就會觸發該事件

事件處理方式:

1 [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];

 

四、UIPickerView

一、UIPickerView經常使用的方法:

UIPickerView代理:

1 @property(nonatomic, assign) id<UIPickerViewDelegate> delegate;
2 @property(nonatomic, assign)id<UIPickerViewDataSource> dataSource;

delegate定義了UIPickerView的外觀和屬性;
dataSource定義了UIPickerView的數據源和定製內容

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

  返回component列、row行的一個UIView,這裏只有在定製的狀況下才有效,其餘狀況返回nil;

- (void)reloadAllComponets;

- (void)reloadComponent:(NSInteger)component;

  從新裝載整個UIPickerView全部列的數據和指定列的數據;

- (void)selectROw:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

  選中UIPickerView中component列、row行,也就是讓改行滾動到中央;

- (void)(NSInteger)selectedRowInComponent:(NSInteger)component;

  返回指定列component中選中的行,沒有選中返回-1;

- (NSInteger)numberOfComponentsInPickerView;

  返回UIPickerView一共有幾列;

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponet:(NSInteger)component;

  返回指定的component列有幾行數據;

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

  返回UIPickerView中component列的寬度;

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponnet:(NSInteget)component;

  返回UIPickerView中component列中每行的高度;

- (void)pickerView:(UIPickerView *) pickView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

  當列component、行row選中的回調函數;

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

  標準的UIPickerView內容,只有字符串;

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

  自定義UIPickerView內容,給每個行、列設置一個UIView對象。(PS:上面兩個方法只能二選一)

二、UIPickerViewDelegate定製:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

  自定義的UIPickerView內容,給每個列、行設置一個UIView對象。

這裏爲列component、行row返回一個UIView用來顯示在UIPickerView中。reusingView:(UIView *)view是iOS內核傳入一個緩存的UIView。在程序中能夠不用分配UIView,而能夠重用iOS傳過來的view。

相關文章
相關標籤/搜索