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

[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
 



示图:



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值