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主要属性:
text:设置UILabel的文本内容,NSString类型;
font:设置文本的字体,UIFont类型;
textColor:设置文本的颜色,UIColor类型;
lineBreakMode:设置折行的模式,UILineBreakMode类型,一般为UILineBreakModeWordWrap;
textAlignment:设置文本的对齐方式,UITextAlignment,有左、中、右
2、UIButton
继承于UIControl基类
UIButton缺省是圆角按钮,还有图片按钮、Info light、Info dark、Contack add、Detail disclosure
Button的点击事件中获取点击Button对象
1 - (IBAction) buttonClick:(id)sender
2 {
3 //将sender强制转换成Button类型,获取哪个按钮触发的点击事件
4 UIButton *button = (UIButton *)sender;
}
- (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知道这是一个按钮的点击事件。
3、NSDatePicker
1、NSDate类:是系统一个日期、时间类。
+(id)date:返回当前的日期、时间;
+(id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs:返回未来secs秒后的日期、时间;
+(id)distantFuture:未来永远达不到的时间;
+(id)distantPast:过去的时间。
2、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日
3、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;
4、UIDatePicker
UIDatePicker事件处理:
UIControlEventValueChanged:UIDatePicker每次值改变时就会触发该事件
事件处理方式:
1 [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
4、UIPickerView
1、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:上面两个方法只能二选一)
2、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。
网友评论