如何自定义PickerView 以及改变字体大小和字体颜色

原创 2016年03月10日 14:19:37



首先,我们要向系统给我们的PickerView一样,定义它的代理方法和数据源方法。

#import <UIKit/UIKit.h>


@class MyPickerView;

//数据源方法

@protocol UIMyPickerDataSource <NSObject>

@required

- (NSInteger)numberOfComponentsInMyPickerView:(nonnull MyPickerView *)myPickerView;//包含列数

@required

- (NSInteger)myPickerView:(nonnullMyPickerView *)myPickerView numberOfRowInComponent:(NSInteger)component;//某一列的行数

@required

- (nullable NSString *)myPickerView:(nonnullMyPickerView *)myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

@end


//代理方法

@protocol UIMyPickerDelegate <NSObject>

- (void)myPickerView:(nonnullMyPickerView *)myPickerView titleForDidSelectRow:(NSInteger)row forComponent:(NSInteger)component;

@end


@interface MyPickerView : UIView

@property (nonatomic,strong)NSMutableArray *_Nonnull ScrolleViewArray;

@property (nonatomic,assign)_Nonnull id <UIMyPickerDataSource> dataSource;

@property (nonatomic,assign)_Nonnull id <UIMyPickerDelegate> delegate;

@end


然后我们来理一下思路。

其次我写的这个是有五行,三列,每列用了5个scrollview。


#import "MyPickerView.h"


@interface MyPickerView()<UIScrollViewDelegate>

@end


@implementation MyPickerView{

    NSArray *_dataArray;

    NSInteger _components;

    CGFloat _height;

    CGFloat _width;

    CGFloat _componentWidth;

    CGFloat _rowHeight;

    NSInteger _flagRow;

    NSInteger _lastFlagRow;

    CGFloat _ScrollRowHeight;

}

/*

 // Only override drawRect: if you perform custom drawing.

 // An empty implementation adversely affects performance during animation.

 - (void)drawRect:(CGRect)rect {

 // Drawing code

 }

 */

- (instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        _ScrolleViewArray = [[NSMutableArrayalloc]init];

        _flagRow = 0;

        _lastFlagRow = 0;

        self.userInteractionEnabled =YES;

        self.clipsToBounds =YES;

        _height = 140;

        _ScrollRowHeight =_height/5.00;

        _width = frame.size.width-60;

        self.backgroundColor = [UIColorwhiteColor];

    }

    return self;

}


-(void)setDataSource:(id<UIMyPickerDataSource>)dataSource{

    

    _dataArray =@[@"",@"",@""];

    _dataSource = dataSource;

    _components = [_dataSourcenumberOfComponentsInMyPickerView:self];

    _componentWidth = ((_width-_components-1)*100)/(_components*100.00);

    

    UIView *leftView= [[UIViewalloc]initWithFrame:CGRectMake(0,55, 40, 40)];

    leftView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

    leftView.layer.cornerRadius =10;

    [self addSubview:leftView];

    for (NSInteger i =0; i<_components+1; i++) {

        UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),10, 1, _height)];

        label.backgroundColor = [UIColorcolorWithRed:127/255.0green:127/255.0blue:127/255.0alpha:0.8];

        [self addSubview:label];

        

        if (i!=_components) {

            NSInteger _rows = [_dataSourcemyPickerView:selfnumberOfRowInComponent:i];

            //NSLog(@"_rows--%ld",_rows);

            UIScrollView *bottomScrollView1 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),5, _componentWidth,20)];

            //bottomScrollView1.delegate = self;

            bottomScrollView1.tag = 501+i*10;

            bottomScrollView1.decelerationRate =0.1;

            bottomScrollView1.showsHorizontalScrollIndicator =NO;

            bottomScrollView1.showsVerticalScrollIndicator =NO;

            bottomScrollView1.bounces = NO;

            bottomScrollView1.alwaysBounceHorizontal =NO;

            bottomScrollView1.alwaysBounceHorizontal =NO;

            bottomScrollView1.userInteractionEnabled =NO;

            

            bottomScrollView1.backgroundColor = [UIColorwhiteColor];

            

            bottomScrollView1.contentSize = CGSizeMake(_componentWidth, 20*_rows);

            bottomScrollView1.contentOffset = CGPointMake(0, -40);

            

            [self addSubview:bottomScrollView1];

            

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];

                [bottomScrollView1 addSubview:label];

                label.tag = 600+(100*i)+j;

                label.alpha = 0.3;

                label.font = [UIFontsystemFontOfSize:16];

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            UIScrollView *middleScrollView1 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(bottomScrollView1.frame),_componentWidth, 30)];

            //middleScrollView1.delegate = self;

            middleScrollView1.tag = 502+i*10;

            middleScrollView1.decelerationRate =0.1;

            middleScrollView1.showsHorizontalScrollIndicator =NO;

            middleScrollView1.showsVerticalScrollIndicator =NO;

            middleScrollView1.bounces = NO;

            middleScrollView1.alwaysBounceHorizontal =NO;

            middleScrollView1.alwaysBounceHorizontal =NO;

            middleScrollView1.userInteractionEnabled =NO;

            

            middleScrollView1.backgroundColor = [UIColorwhiteColor];

            

            middleScrollView1.contentSize = CGSizeMake(_componentWidth, 30*(_rows));

            middleScrollView1.contentOffset = CGPointMake(0, -30);

            

            [self addSubview:middleScrollView1];

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];

                [middleScrollView1 addSubview:label];

                label.tag = 600+(100*i)+j+20;

                label.alpha = 0.5;

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.font = [UIFontsystemFontOfSize:25];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            

            

            UIScrollView *topScrollView =[[ UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),55, _componentWidth,40)];

            //topScrollView.delegate = self;

            topScrollView.tag = 503+i*10;

            topScrollView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

            

            

            topScrollView.decelerationRate = 0.1;

            topScrollView.showsHorizontalScrollIndicator =NO;

            topScrollView.showsVerticalScrollIndicator =NO;

            topScrollView.bounces = NO;

            topScrollView.userInteractionEnabled =NO;

            topScrollView.alwaysBounceHorizontal =NO;

            topScrollView.alwaysBounceHorizontal =NO;

            topScrollView.contentOffset = CGPointMake(0, 0);

            topScrollView.contentSize = CGSizeMake(_componentWidth, 40*_rows);

            [self addSubview:topScrollView];

            for (NSInteger j =0; j<_rows; j++) {

                //NSLog(@"j==%ld",j);

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,40*j, _componentWidth,40)];

                [topScrollView addSubview:label];

                label.tag = 600+(100*i)+j+40;

                label.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];

                label.font = [UIFontsystemFontOfSize:34];

                label.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:0];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:jforComponent:i];//[NSString stringWithFormat:@"%ld",label.tag];//

            }

            

            UILabel *unitLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(topScrollView.frame)-25,CGRectGetMaxY(topScrollView.frame)-20,20, 20)];

            [self addSubview:unitLabel];

            unitLabel.text = _dataArray[i];

            //unitLabel.backgroundColor = [UIColor whiteColor];

            unitLabel.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];

            unitLabel.font = [UIFontboldSystemFontOfSize:16];

            

            UIScrollView *middleScrollView2 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(topScrollView.frame),_componentWidth, 30)];

            //middleScrollView2.delegate = self;

            middleScrollView2.tag = 504+i*10;

            middleScrollView2.decelerationRate =0.1;

            middleScrollView2.showsHorizontalScrollIndicator =NO;

            middleScrollView2.showsVerticalScrollIndicator =NO;

            middleScrollView2.bounces = NO;

            middleScrollView2.alwaysBounceHorizontal =NO;

            middleScrollView2.alwaysBounceHorizontal =NO;

            middleScrollView2.userInteractionEnabled =NO;

            

            middleScrollView2.backgroundColor = [UIColorwhiteColor];

            

            middleScrollView2.contentOffset = CGPointMake(0, 30);

            [self addSubview:middleScrollView2];

            middleScrollView2.contentSize = CGSizeMake(_componentWidth, 30*(_rows));

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];

                [middleScrollView2 addSubview:label];

                label.tag = 600+(100*i)+j+60;

                label.alpha = 0.5;

                label.font = [UIFontsystemFontOfSize:25];

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            

            //27376

            

            UIScrollView *bottomScrollView2 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(middleScrollView2.frame),_componentWidth, 20)];

            //bottomScrollView2.delegate = self;

            bottomScrollView2.tag = 505+i*10;

            bottomScrollView2.decelerationRate =0.1;

            bottomScrollView2.showsHorizontalScrollIndicator =NO;

            bottomScrollView2.showsVerticalScrollIndicator =NO;

            bottomScrollView2.bounces = NO;

            bottomScrollView2.alwaysBounceHorizontal =NO;

            bottomScrollView2.alwaysBounceHorizontal =NO;

            bottomScrollView2.userInteractionEnabled =NO;

            

            bottomScrollView2.backgroundColor = [UIColorwhiteColor];

            

            bottomScrollView2.contentOffset = CGPointMake(0, 40);

            bottomScrollView2.contentSize = CGSizeMake(_componentWidth, 20*_rows);

            [self addSubview:bottomScrollView2];

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];

                [bottomScrollView2 addSubview:label];

                label.tag = 600+(100*i)+j+80;

                label.alpha = 0.3;

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.font = [UIFontsystemFontOfSize:16];

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            UIScrollView *_scrollView= [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),0, _componentWidth,_height)];

            _scrollView.userInteractionEnabled =YES;

            [self addSubview:_scrollView];

            _scrollView.tag = 506+i*10;

            

            _scrollView.decelerationRate = 0.01;

            //_scrollView.showsHorizontalScrollIndicator = NO;

            _scrollView.showsVerticalScrollIndicator =NO;

            _scrollView.bounces = NO;

            _scrollView.contentSize = CGSizeMake(_componentWidth, 40*(_rows+2.5));

            

            _scrollView.contentOffset = CGPointMake(0, 0);

            _scrollView.delegate = self;


            

            NSArray *array = @[bottomScrollView1,middleScrollView1,topScrollView,middleScrollView2,bottomScrollView2];

            [_ScrolleViewArray addObject:array];

            

        }

        if(i==_components-1){

            UIView *rightView= [[UIViewalloc]initWithFrame:CGRectMake(_width+20,55, 40, 40)];

            rightView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

            rightView.layer.cornerRadius =10;

            [self addSubview:rightView];

        }

        

    }

}

- (void)setDelegate:(id<UIMyPickerDelegate>)delegate{

    _delegate = delegate;

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offSet = scrollView.contentOffset.y;

    CGFloat _offset = offSet/40.00;

    _flagRow = (NSInteger)(offSet/40.00+0.5);

    if (_flagRow!=_lastFlagRow) {

        _lastFlagRow =_flagRow;

       // NSLog(@"_flagRow%ld",_flagRow);

    }

    //NSLog(@"tag_%ld",scrollView.tag);

    

    NSInteger flag = scrollView.tag-500;

    NSInteger componentFlag = flag/10;

    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];

    for (NSInteger i=0; i<5; i++) {

        UIScrollView *scrollView = (UIScrollView *)array[i];


        if (i==0){

            scrollView.contentOffset = CGPointMake(0, 20*_offset-40);

        }

        if(i==1) {

            scrollView.contentOffset = CGPointMake(0, 30*_offset-30);

        }

        if (i==2){

            scrollView.contentOffset = CGPointMake(0, offSet);

        }

        if(i==3) {

            scrollView.contentOffset = CGPointMake(0, 30*_offset+30);

        }

        if(i==4){

            scrollView.contentOffset = CGPointMake(0, 20*_offset+40);

        }

    }

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    NSLog(@"停止拖拽");

    [self setUpDelegateWith:scrollView];

}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    NSLog(@"开始减速");

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    

    NSLog(@"减速结束");

    [self setUpDelegateWith:scrollView];

    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    //        if (_delegate && [_delegate respondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {

    //            [_delegate myPickerView:self titleForDidSelectRow:_offSetY forComponent:componentFlag];

    //        }

    //    });

}


-(void)setUpDelegateWith:(UIScrollView *)scrollView{

    CGFloat offSetY = scrollView.contentOffset.y;

    NSInteger _offSetY = offSetY/40.00+0.5;

    NSInteger flag = scrollView.tag-500;

    NSInteger componentFlag = flag/10;

    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];

    for (NSInteger i=0; i<5; i++) {

        UIScrollView *scrollView = (UIScrollView *)array[i];

        

        if (i==0){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,20*_offSetY-40)animated:YES];

        }

        if(i==1) {//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,30*_offSetY-30)animated:YES];

        }

        if (i==2){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,40*_offSetY) animated:YES];

        }

        if(i==3) {//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,30*_offSetY+30)animated:YES];

        }

        if(i==4){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,20*_offSetY+40)animated:YES];

        }

    }

    if (_delegate && [_delegaterespondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {

        [_delegatemyPickerView:selftitleForDidSelectRow:_offSetY forComponent:componentFlag];}

}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    NSLog(@"滑动动画结束");

}

@end


代码写的比较啰嗦,但是一般人都能看懂,有人需要就用一下吧

UIPickerView控件中自定义显示的字体大小及样式

//重写方法 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(...
  • jiadabin
  • jiadabin
  • 2016年09月27日 15:24
  • 4182

UIPickerView 行的大小和字体大小

UIPickerView 设计每行的大小 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent...
  • qq_30023981
  • qq_30023981
  • 2017年02月04日 16:04
  • 2434

uipickerView如何设置字体大小

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusi...
  • lanzeng_in_sz
  • lanzeng_in_sz
  • 2015年11月10日 16:27
  • 3179

swift3.0 修改UISegmentedControl的字体大小

var segment = UISegmentControl //修改字体大小 let font = UIFont.systemFont(ofSize: 20) segment.setTitle...
  • amberoot
  • amberoot
  • 2017年10月27日 14:01
  • 145

PickView选中某一行的字体变化

要求是选中某一行的时候字体变化,代码如下 .h @property (assign,nonatomic)NSInteger selectedRow; .m - (void)viewDidLoad {...
  • u013705509
  • u013705509
  • 2015年12月04日 14:18
  • 1531

使用Android-PickerView修改item的字体大小(坑)

这里记录一个使用Android-PickerView修改字体大小的方法。默认字体大小是20sp,天坑啊。...
  • z_zT_T
  • z_zT_T
  • 2016年12月21日 17:57
  • 1555

Swift开发:Swift 修改UISegmentedControl字体大小,颜色

1 Swift code // // ViewController.swift // Test // // Created by 开发 on 2017/10/27. // Copyright ...
  • taoerit
  • taoerit
  • 2017年10月27日 13:54
  • 162

swift中UIPickerView的使用

https://github.com/potato512/SYSwiftLearning 示例代码如下: 1、参数定义 let width:CGFloat = (UIScreen.m...
  • potato512
  • potato512
  • 2016年09月29日 17:40
  • 2821

利用Runtime修改UIdatePicker的字体颜色

通过修改UIdatePicker的字体颜色来认清Runtime的真相 用过苹果原生UIdatePicker的Ios开发者都知道,UIdatePicker这个时间选择器的字体颜色默认的是黑色,并且官方A...
  • u013263917
  • u013263917
  • 2015年09月01日 15:47
  • 2162

自定义spinner实现修改字体大小、颜色等

spinner默认显示的字体颜色为纯黑色,在一些情况下,我们想要改变这种颜色,但是却发现在spinner的属性里面并没有setTextColor类似这样的属性,也就是说我们无法简单的直接设置其内部字体...
  • u011835956
  • u011835956
  • 2014年01月21日 11:27
  • 1650
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何自定义PickerView 以及改变字体大小和字体颜色
举报原因:
原因补充:

(最多只允许输入30个字)