[IOS]UIPickerView(自定义选择器)
2014-03-23 21:06
459 查看
[IOS]UIPickerView(自定义选择器)
Demo:http://download.csdn.net/detail/u012881779/8645725
示图:
Demo:http://download.csdn.net/detail/u012881779/8645725
#import <UIKit/UIKit.h> @interface WAFontStyle : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> @property (strong, nonatomic) NSMutableArray *wFontColor;//字体颜色 @property (strong, nonatomic) NSMutableArray *wFont;//字体类型 @property (strong, nonatomic) NSMutableArray *wFontSize;//字体大小 @property (nonatomic) float wChioceSize;//选择字体大小 @property (weak, nonatomic) IBOutlet UIPickerView *wFontPickerView; @property (weak, nonatomic) IBOutlet UILabel *wFontLab; @property (weak, nonatomic) IBOutlet UIView *wFontView; @end @implementation WAFontStyle @synthesize wFontColor = _wFontColor; @synthesize wFont = _wFont; @synthesize wFontSize = _wFontSize; - (void)viewDidLoad { [super viewDidLoad]; [_wFontView.layer setCornerRadius:20]; /* *数据准备 */ //字体类型 _wFont = (NSMutableArray *)[UIFont familyNames]; //字体颜色 _wFontColor = [[NSMutableArray alloc] initWithObjects: [UIColor greenColor], [UIColor blackColor], [UIColor grayColor], [UIColor redColor], [UIColor blueColor], [UIColor whiteColor], [UIColor yellowColor], [UIColor brownColor], [UIColor orangeColor], [UIColor magentaColor], [UIColor purpleColor], nil]; //字体大小 _wFontSize = [[NSMutableArray alloc] initWithObjects:@"12",@"14",@"16",@"18",@"20",@"22",@"24",@"26",@"28",@"30",nil]; //初始默认选择 for(int i = 0;i < 3;i ++){ int row = 0; if(i == 0) row = (int)[_wFont count]/2; else if(i == 1) row = (int)[_wFontSize count]/2; else if(i == 2) row = (int)[_wFontColor count]/2; [_wFontPickerView selectRow:row inComponent:i animated:YES]; } } //选择取消 - (IBAction)mCancelAction:(id)sender { [self.view removeFromSuperview]; } //选择确定 - (IBAction)mSelectorAction:(id)sender { [self mCancelAction:nil]; } #pragma mark UIPickerViewDataSource //几列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 3; } //几行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if(component == 0) return [_wFont count]; else if(component == 1) return [_wFontSize count]; else if(component == 2) return [_wFontColor count]; return -1; } #pragma mark UIPickerViewDelegate //component宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if(component == 0) return 150.0f; else if(component == 1) return 50.0f; else if(component == 2) return 50.0f; return 0.0f; } //row高度 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 50.0f; } //专门为定制UIPickerView用的一个函数,返回component列row行所在的定制的View,不自定义的话会有一个系统默认的格式 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //得到Component对应的宽和高 CGFloat width = [self pickerView:pickerView widthForComponent:component]; CGFloat height = [self pickerView:pickerView rowHeightForComponent:component]; //返回UIView UIView *returnView = [[UIView alloc] init]; [returnView setFrame:CGRectMake(0, 0, width, height-10)]; //添加UILabel到UIView上,传递数据 UILabel *label = [[UILabel alloc] init]; label.frame = returnView.frame; [label setTextColor:[UIColor blackColor] ]; label.tag = 1000; [label setFont:[UIFont systemFontOfSize:20]]; [returnView addSubview:label]; //对Label附加数据 if(component == 0) label.text = [_wFont objectAtIndex:row];//字体 else if(component == 1) label.text = [_wFontSize objectAtIndex:row];//大小 else if(component == 2) label.backgroundColor = [_wFontColor objectAtIndex:row];//颜色 return returnView; } //关联UILabel 和 UIPickerView - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ //取得选择的Row NSInteger rowZero,rowOne,rowTwo; rowZero = [pickerView selectedRowInComponent:0]; rowOne = [pickerView selectedRowInComponent:1]; rowTwo = [pickerView selectedRowInComponent:2]; //从选择的Row取得View UIView *viewZero,*viewOne,*viewTwo; viewZero = [pickerView viewForRow:rowZero forComponent:0]; viewOne = [pickerView viewForRow:rowOne forComponent:1]; viewTwo = [pickerView viewForRow:rowTwo forComponent:2]; //从取得的View取得上面UILabel UILabel *labZero,*labOne,*labTwo; labZero = (UILabel *)[viewZero viewWithTag:1000]; labOne = (UILabel *)[viewOne viewWithTag:1000]; labTwo = (UILabel *)[viewTwo viewWithTag:1000]; //将从三列分别取得的,字体,大小,颜色,传递给在界面上显示的UILabel [_wFontLab setFont:[UIFont fontWithName:labZero.text size:[labOne.text floatValue]]]; _wChioceSize = [labOne.text floatValue]; [_wFontLab setTextColor:labTwo.backgroundColor]; } @end
示图:
相关文章推荐
- iOS之UIPickerView的使用--单列选择器
- (转)IOS之简单选择器UIPickerView(省份+城市)
- iOS开发之UIPickerView实现城市选择器的步骤详解
- ios iphone 自定义UIPickerView
- UIPickerView 选择控制器----ios控件 UIPickerView应用 制作简易省、市、地区选择器
- iOS之UIPickerView的使用--多列选择器
- IOS之简单选择器UIPickerView(省份+城市)
- iOS之UIPickerView的使用--多列关联选择器
- 【IOS 开发学习总结-OC-48】★★ios开发之UI控件——UIDatePicker与 UIPickerView
- iOS--UIPickerView
- iOS学习之UIPickerView控件的关联选择
- iOS UIPickerView
- IOS开发-UIDatePicker和UIPickerView(拾取器)
- Android 自定义View,实现滚动选择器PickerView
- ios 炒鸡简单使用地址选择器(AddressPickerView),支持省份,省市,省市区三种联动
- ios中修改自定义PickerView的字体颜色
- iOS进度条 自定义圆角 UIProgressView
- UIPickerView实现年月选择器
- iOS学习笔记01-自定义简单弹出pickerView
- iOS App中UIPickerView选择栏控件的使用实例解析