CustomPickerView

//
//  CustomPickerViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/18.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "CustomPickerViewController.h"

@interface CustomPickerViewController (){
   
    UILabel *label;
    UIPickerView *myPickerView;
    
    NSArray *fonts;
    NSArray *colors;
    NSArray *sizes;


}

@end

@implementation CustomPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.title = @"选择字体";
    [self createView:self.view];
}

- (void)createView:(id)uiView{
    UIView *view = (UIView *)uiView;
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 80, 300, 80);
    label.backgroundColor = [UIColor whiteColor];
    
    
    myPickerView = [[UIPickerView alloc] init];
    myPickerView.frame = CGRectMake(0, 200, 500, 200);
    myPickerView.backgroundColor = [UIColor whiteColor];
    
    fonts = [UIFont familyNames];
    colors = [NSArray arrayWithObjects:[UIColor blackColor],[UIColor whiteColor],[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor blueColor],[UIColor purpleColor], nil];
    sizes = [NSArray arrayWithObjects:@"10",@"20",@"30",@"40",@"50",@"60",@"70", nil];
    
    myPickerView.delegate = self;
    myPickerView.dataSource = self;
    myPickerView.backgroundColor = [UIColor orangeColor];
    
    
    // 设置  myPickerView 得内容为中间数据
    for (int i = 0; i < 3; i++) {
        long row = 0;
        if (i == 0) {
            row = [fonts count]/1;
        }else if(i == 1){
            row = [colors count]/3;
        }else{
            row = [sizes count]/2;
        }
        // 选中
        [myPickerView selectRow:row inComponent:i animated:true];
    }
    
    
    
    
    
    [view addSubview:label];
    [view addSubview:myPickerView];

}

// 返回3列
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 3;
}

// 这一列有几行
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return [fonts count];
    }else if(component == 1){
        return [colors count];
    }else if(component == 2){
        return  [sizes count];
    }
    return -1;
}

// 设置列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component == 0) {
        return 200.0f;
    }else if(component == 1){
        return 80.0f;
    }else if(component == 2){
        return 120.0f;
    }
    return 0.0f;
}

// 设置列的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 100.0f;

}

// 返回component列row行返回的UIView视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
 
    CGFloat height = [self pickerView:pickerView rowHeightForComponent:component];
    CGFloat width = [self pickerView:pickerView widthForComponent:component];
    
    UIView *myview = [[UIView alloc] init];
    myview.frame = CGRectMake(0, 0, width, height-20);
    
    UILabel *myLabel = [[UILabel alloc] init];
    myLabel.frame = myview.frame;
    myLabel.textAlignment = NSTextAlignmentLeft;
    myLabel.backgroundColor = [UIColor orangeColor];
    myLabel.tag = 200;
 
    if (component == 0) {
        myLabel.text = [fonts objectAtIndex:row];
    }else if (component == 1){
       myLabel.backgroundColor = [colors objectAtIndex:row];
    }else{
        myLabel.text = [sizes objectAtIndex:row];
    }
    [myview addSubview:myLabel];
    return myview;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    // 找出每一列选中的行
    NSInteger  fontRow = [pickerView selectedRowInComponent:0];
    NSInteger  colorRow = [pickerView selectedRowInComponent:1];
    NSInteger  sizeRow = [pickerView selectedRowInComponent:2];
 
    // 选中的都是一个视图 UIView
    UIView *fontView,*colorView,*sizeView;
    fontView = [pickerView viewForRow:fontRow forComponent:0];
    colorView = [pickerView viewForRow:colorRow forComponent:1];
    sizeView = [pickerView viewForRow:sizeRow forComponent:2];
    
    
    // Label这个对象只是一个中间桥梁,存储数据而已,没有什么其他意义
    UILabel *fontLabel,*colorLabel,*sizeLabel;
    fontLabel = (UILabel *)[fontView viewWithTag:200];
    colorLabel = (UILabel *)[colorView viewWithTag:200];
    sizeLabel = (UILabel *)[sizeView viewWithTag:200];
    
    
    label.text =  fontLabel.text;
    label.textColor = colorLabel.backgroundColor;
    label.font = [UIFont fontWithName:fontLabel.text size:[sizeLabel.text floatValue]];
    
    
    
   
    
    
}



@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值