//
// 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
CustomPickerView
最新推荐文章于 2024-05-11 10:06:35 发布