本文主要是介绍[IOS]UIPickerView(自定义选择器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[IOS]UIPickerView(自定义选择器)
#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];//返回UIViewUIView *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{//取得选择的RowNSInteger rowZero,rowOne,rowTwo;rowZero = [pickerView selectedRowInComponent:0];rowOne = [pickerView selectedRowInComponent:1];rowTwo = [pickerView selectedRowInComponent:2];//从选择的Row取得ViewUIView *viewZero,*viewOne,*viewTwo;viewZero = [pickerView viewForRow:rowZero forComponent:0];viewOne = [pickerView viewForRow:rowOne forComponent:1];viewTwo = [pickerView viewForRow:rowTwo forComponent:2];//从取得的View取得上面UILabelUILabel *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(自定义选择器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!