您的位置:首页 > 产品设计 > UI/UE

[IOS]UIPickerView(自定义选择器)

2014-03-23 21:06 459 查看
[IOS]UIPickerView(自定义选择器)

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
 


示图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: