如何自定义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:43
  • 4748

UIPickerView 行的大小和字体大小

UIPickerView 设计每行的大小 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent...
  • qq_30023981
  • qq_30023981
  • 2017-02-04 16:04:31
  • 3147

iOS UIPickerView的常见属性和方法

//联系人:石虎  QQ: 1224614774 昵称:嗡嘛呢叭咪哄 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerVie...
  • shihuboke
  • shihuboke
  • 2017-06-09 08:39:00
  • 682

iOS开发 - 改变picker选中行字体颜色

平日里的picker都适用默认的,也没有刻意去自定义什么,原生的控件比如picker,还是很美观的,真要自己去改变点什么就是给自己找不自在了,简单的自定义如颜色,字体,分割线颜色还可以调,如果涉及到更...
  • CodingFire
  • CodingFire
  • 2017-09-18 16:46:11
  • 1512

ios中修改自定义PickerView的字体颜色

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)...
  • TianYou_Code
  • TianYou_Code
  • 2016-11-19 16:31:20
  • 2389

iOS 改变Label字号大小

label.font = [UIFont systemFontOfSize:你想设置的大小]
  • saw471
  • saw471
  • 2016-05-10 14:42:47
  • 766

selectpicker多选功能实现

1.前台设置 需要设置selectpicker为multiple,见代码 ...
  • yzhldk
  • yzhldk
  • 2015-07-21 09:18:22
  • 5491

selectpicker clone的问题

selectpicker clone的问题需求: 在table中存在一行,含有 一个selectpicker 的select .点击添加,clone 新增同样的一行。 Bug: 当点击新clon...
  • lt289597
  • lt289597
  • 2016-11-10 17:28:26
  • 837

修改PickerView选中行背景色

实现图中效果,背景色为淡灰色,去掉上下两条线创建UIPickerView的类 作为基类//.h文件 #import @interface BasePickerView : UIPickerView ...
  • dream9393
  • dream9393
  • 2017-07-22 17:51:03
  • 891

iOS - UIPickerView文字大小颜色修改,无限轮播,无限循环滚动

最近项目里用到了UIPickerView,要求无限轮播,简单看了看UIPickView的所有属性和所有代理方法,没有无限轮播的设置,遂去百度了一把,发现网上流传着一个帖子,大家各种转载,看了看后,明白...
  • icefishlily
  • icefishlily
  • 2016-09-29 10:31:26
  • 3447
收藏助手
不良信息举报
您举报文章:如何自定义PickerView 以及改变字体大小和字体颜色
举报原因:
原因补充:

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