//

rgb颜色转换(16进制->10进制)

#define

UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue

& 0xFF0000)

>> 16))/255.0

green:((float)((rgbValue

& 0xFF00)

>> 8))/255.0

blue:((float)(rgbValue

& 0xFF))/255.0

alpha:1.0]

#define MAIN_HEIGHT [UIScreen

mainScreen].bounds.size.height

#define MAIN_WIDTH

[UIScreen

mainScreen].bounds.size.width

#define

MainColor UIColorFromRGB(0x4887CA)

#import

"CustomDatePicker.h"

#import

"UIPickerView+malPicker.h"

@interface

CustomDatePicker

()

@property

(nonatomic,strong)

NSArray

*yearArr

, *monthArr , *dayArr;

@property

(nonatomic,strong)

NSString

*yearNow

, *monthNow , *dayNow;

// 当前

'年'

'月'

'日'

数组中所占的索引值

@property

(nonatomic,assign)

NSInteger

rowLeft ,

rowMiddle , rowRight;

// the year and month of displaying on view

now

@property

(nonatomic,assign)

NSInteger

chosenYear ,

chosenMonth , chosenDay;

@end

@implementation

CustomDatePicker

-(instancetype)init

{

self

=

[super

init];

if

(self)

{

[self

setup];

}

return

self;

}

-(void)setup

{

[self

timeNow];

[self

timeArray];

_pickerView

=

[[UIPickerView

alloc]

initWithFrame:CGRectMake(0,

0,

MAIN_WIDTH-40,

100)];

_pickerView.dataSource

=

self;

_pickerView.delegate

=

self;

[self

addSubview:_pickerView];

[self

defaultDisplay];

}

-(void)timeArray

{

NSInteger

yearN =

[_yearNow

integerValue];

_yearArr

=

[[NSMutableArray

alloc]

initWithObjects:

[self

stringCombine:[NSString

stringWithFormat:@"%ld",yearN-4]

string:@"年"],

[self

stringCombine:[NSString

stringWithFormat:@"%ld",yearN-3]

string:@"年"],

[self

stringCombine:[NSString

stringWithFormat:@"%ld",yearN-2]

string:@"年"],

[self

stringCombine:[NSString

stringWithFormat:@"%ld",yearN-1]

string:@"年"],

[self

stringCombine:[NSString

stringWithFormat:@"%ld",yearN]

string:@"年"],

nil];

_monthArr

=

@[@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月",];

_dayArr

=

@[@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",@"31日"];

}

-(void)timeNow

{

// 当前时间

选择器默认显示时间

NSDate*

date = [NSDate

date];

NSDateFormatter

*fm

=[[NSDateFormatter

alloc]

init];

fm.dateFormat

=

@"yyyy";

self.yearNow

=

[fm stringFromDate:date];

self.chosenYear

=

[_yearNow

integerValue];

fm.dateFormat

=

@"MM";

self.monthNow

=

[fm stringFromDate:date];

self.chosenMonth

=

[_monthNow

integerValue];

fm.dateFormat

=

@"dd";

self.dayNow

=

[fm stringFromDate:date];

self.chosenDay

=

[_dayNow

integerValue];

}

-(void)defaultDisplay

{

NSUInteger

rowLeft =

[_yearArr

indexOfObject:[self

stringCombine:_yearNow

string:@"年"]];

NSUInteger

rowMiddle

= [_monthArr

indexOfObject:[self

stringCombine:_monthNow

string:@"月"]];

NSUInteger

rowRight

= [_dayArr

indexOfObject:[self

stringCombine:_dayNow

string:@"日"]];

_rowLeft

=

rowLeft;

_rowMiddle

=

rowMiddle;

_rowRight

=

rowRight;

[_pickerView

selectRow:rowLeft

inComponent:0

animated:YES];

[_pickerView

selectRow:rowMiddle

inComponent:1

animated:YES];

[_pickerView

selectRow:rowRight

inComponent:2

animated:YES];

}

-

(NSInteger)numberOfComponentsInPickerView:(UIPickerView

*)pickerView

{

return

3;

}

-

(NSInteger)pickerView:(UIPickerView

*)pickerView

numberOfRowsInComponent:(NSInteger)component

{

if

(component ==

0)

{

return

_yearArr.count;

}

else

if

(component ==

1)

{

return

_monthArr.count;

}

else

{

if

((self.chosenMonth

==

1)

|| (self.chosenMonth

==

3)

|| (self.chosenMonth

==

5)

||(self.chosenMonth

==

7)||(self.chosenMonth

==

8)||(self.chosenMonth

==

10)||(self.chosenMonth

==

12))

{

return

31;

}

if

((self.chosenMonth

==

4)||(self.chosenMonth

==

6)||(self.chosenMonth

==

9)||(self.chosenMonth

==

11))

{

return

30;

}

if

((self.chosenYear

%

100

!=

0)&(self.chosenYear

%

4

==

0))

{

return

29;

}

if

((self.chosenYear

%

400

==

0))

{

return

29;

}

return

28;

}

}

-

(NSString

*)pickerView:(UIPickerView

*)pickerView

titleForRow:(NSInteger)row

forComponent:(NSInteger)component

{

if

(component ==

0)

{

return[_yearArr

objectAtIndex:row];

}

else

if(component

== 1)

{

return

[_monthArr

objectAtIndex:row];

}

else

{

return

[_dayArr

objectAtIndex:row];

}

return

nil;

}

-

(UIView

*)pickerView:(UIPickerView

*)pickerView

viewForRow:(NSInteger)row

forComponent:(NSInteger)component

reusingView:(UIView

*)view

{

UILabel*

pickerLabel = (UILabel*)view;

if

(!pickerLabel){

pickerLabel = [[UILabel

alloc]

init];

//pickerLabel.minimumFontSize

= 8.;

pickerLabel.adjustsFontSizeToFitWidth

=

YES;

pickerLabel.textAlignment

=

NSTextAlignmentCenter;

pickerLabel.textColor

=

UIColorFromRGB(0x666666);

[pickerLabel setBackgroundColor:[UIColor

clearColor]];

[pickerLabel setFont:[UIFont

systemFontOfSize:17]];

}

// Fill the label

text here

pickerLabel.text=[self

pickerView:pickerView

titleForRow:row

forComponent:component];

[pickerView clearSpearatorLine];

return

pickerLabel;

}

-

(void)pickerView:(UIPickerView

*)pickerView

didSelectRow:(NSInteger)row

inComponent:(NSInteger)component

{

NSString

*year ,

*month , *day;

if

(component ==

0)

{

_rowLeft

=

row;

[self

commonYearOrLeapYear];

}

if

(component ==

1)

{

_rowMiddle

=

row;

[self

numberOfDaysEachMonth];

}

if

(component ==

2)

{

_rowRight

=

row;

}

year = _yearArr[_rowLeft];

month = _monthArr[_rowMiddle];

day = _dayArr[_rowRight];

NSArray

*yearArr

= [year componentsSeparatedByString:@"年"];

NSArray

*monthArr

= [month componentsSeparatedByString:@"月"];

NSArray

*dayArr

= [day componentsSeparatedByString:@"日"];

self.chosenYear

=

[yearArr[0]

integerValue];

self.chosenMonth

=

[monthArr[0]

integerValue];

self.chosenDay

=

[dayArr[0]

integerValue];

//

刷新

[pickerView reloadComponent:2];

//NSLog(@"current

date pick %@%@%@",year,month,day);

if

(self.delegate

&&

[self.delegate

respondsToSelector:@selector(CustomPickerView:didSelectRow:inComponent:year:month:day:)])

{

[self.delegate

CustomPickerView:pickerView

didSelectRow:row

inComponent:component

year:year

month:month

day:day];

}

}

-(void)commonYearOrLeapYear

{

NSString

*year

= _yearArr[_rowLeft];

NSArray

*strArr

= [year componentsSeparatedByString:@"年"];

self.chosenYear

=

[strArr[0]

integerValue];

self.chosenMonth

=

_rowMiddle+1;

if

((self.chosenMonth

==

1)

|| (self.chosenMonth

==

3)

|| (self.chosenMonth

==

5)

||(self.chosenMonth

==

7)||(self.chosenMonth

==

8)||(self.chosenMonth

==

10)||(self.chosenMonth

==

12))

{

}

else

if

((self.chosenMonth

==

4)||(self.chosenMonth

==

6)||(self.chosenMonth

==

9)||(self.chosenMonth

==

11))

{

if

(_rowRight

>

29)

{

_rowRight

=

29;

}

}

else

if

((self.chosenYear

%

100

!=

0)&(self.chosenYear

%

4

==

0))

{

if

(_rowRight

>

28)

{

_rowRight

=

28;

}

}

else

if

((self.chosenYear

%

400

==

0))

{

if

(_rowRight

>

28)

{

_rowRight

=

28;

}

}

else

if

(_rowRight

>

27)

{

_rowRight

=

27;

}

}

-(void)numberOfDaysEachMonth

{

NSString

*year

= _yearArr[_rowLeft];

NSArray

*strArr

= [year componentsSeparatedByString:@"年"];

self.chosenYear

=

[strArr[0]

integerValue];

self.chosenMonth

=

_rowMiddle+1;

if

((self.chosenMonth

==

1)

|| (self.chosenMonth

==

3)

|| (self.chosenMonth

==

5)

||(self.chosenMonth

==

7)||(self.chosenMonth

==

8)||(self.chosenMonth

==

10)||(self.chosenMonth

==

12))

{

}

else

if

((self.chosenMonth

==

4)||(self.chosenMonth

==

6)||(self.chosenMonth

==

9)||(self.chosenMonth

==

11))

{

if

(_rowRight

>

29)

{

_rowRight

=

29;

}

}

else

if

((self.chosenYear

%

100

!=

0)&(self.chosenYear

%

4

==

0))

{

if

(_rowRight

>

28)

{

_rowRight

=

28;

}

}

else

if

((self.chosenYear

%

400

==

0))

{

if

(_rowRight

>

28)

{

_rowRight

=

28;

}

}

else

if

(_rowRight

>

27)

{

_rowRight

=

27;

}

}

//字符串拼接

-

(NSString

*)stringCombine:(NSString

*)str

string:(NSString

*)string

{

NSString

*stringCom =

[NSString

stringWithFormat:@"%@%@",str,string];

return

stringCom;

}

ios 时间选择器月份设置中文_iOS 自定义日期选择器  PickerView优化版相关推荐

  1. android自定义滚动日期,Android基于wheelView实现自定义日期选择器

    本文实例为大家分享了Android实现自定义日期选择器的具体代码,供大家参考,具体内容如下 项目要求效果图: 要求 "6月20 星期五" 这一项作为一个整体可以滑动,"7 ...

  2. Android基于wheelView的自定义日期选择器(可拓展样式)

    基于wheelView的自定义日期选择器 项目要求效果图: 要求 "6月20 星期五" 这一项作为一个整体可以滑动,"7时"."48分"分别 ...

  3. html5 自定义 datepicker,如何使用 React 构建自定义日期选择器(3)

    本文作者:IMWeb howenhuo 未经同意,禁止转载 Datepicker 组件 构建 Datepicker 组件 要开始构建 Datepicker 组件,请将以下代码片段添加到 src/com ...

  4. 《Android开发卷——自定义日期选择器(三)》

                 继 <Android开发卷--自定义日期选择器(一)>:http://blog.csdn.net/chillax_li/article/details/19047 ...

  5. 《Android开发卷——自定义日期选择器(二)》

    (小米手机) (中兴手机) 在上一篇中,我介绍了一般公司都会自定义时间日期选择器,并结合自己所做的项目给大家参考. 工作实录之<Android开发卷--自定义日期选择器(一)>链接:htt ...

  6. 【iOS】自定义日期选择器

    自定义了一个日期选择器,与大家分享一下,期待宝贵建议. github下载地址:https://github.com/huahua0809/XHDatePicker 下面只是说明一下怎么用,具体实现请下 ...

  7. layui 日期范围选择器_UI设计素材模板|完美日期选择器

    1.日期选择器应该用于什么? "找以往的性能数据.我希望很容易地可以看到某个时期的报告." 2.你需要选择一个具体的日子还是一个范围? "一个范围.大概6个小时,或者一个 ...

  8. 3D模型修补软件Magics的几个基本操作(设置中文、自定义工具页、缩放与拉伸)

    摘要:做3D打印的,一般都会使用Magics软件.不做3D打印的,也有必要了解一下Magics软件,以后会用到的.本文简单介绍一下Magics软件是什么.怎么样设置成中文.工具页丢了怎么找回来,以及常 ...

  9. 多选月份的日期选择器_GitHub - ylmyg/SelectionTime: Android下日期选择器,支持范围选择、多选、单选、根据输入天数选择日期...

    SelectionTime(1.0.1) SelectionTime是用于Android设备上选择日期开源库,高度订制,打造适合自己的日期控件 效果图(多选.范围选.单选) 安装说明 Gradle: ...

最新文章

  1. 6.分布式数据库HBase第2部分
  2. 【深度学习】CornerNet: 将目标检测问题视作关键点检测与配对
  3. python会议室系统预定_python项目篇-酒店(会议室,电影)预定
  4. Aaron Stannard谈Akka.NET 1.1
  5. 【坑】执行Consumer的时候发生java.net.UnknownHostException错误
  6. java a星寻路算法_用简单直白的方式讲解A星寻路算法原理
  7. linux中退格出现乱码,SSH中的SQL命令按退格键出现乱码的问题解决
  8. 与context的关系_Android 一个进程有多少个 Context 对象(答对的不多)
  9. 一将无能,累死三军!数据团队有“会说话”的好领导,有多重要?
  10. 驱动,包括很多软件,并不是最新的就是最好的
  11. 旋转链表 Java,leetcode 旋转链表 Java
  12. oracle 尝试分配内存不足,ORA-04030: 在尝试分配...字节(...)时进程内存不足的原因分析解决方法...
  13. 信号完整性问题的11个基本原则(伯格丁原则)
  14. 发现自己水平很欠缺!
  15. Win10数字权利激活批处理版
  16. JS原生---歌词滚动效果案例
  17. 【已解决】adb connect x.x.x.x:5555报错由于 目标计算机积极拒绝,无法连接
  18. Dart vs Swift
  19. 高项 案例分析重点知识-多读(一)
  20. 使用计算机设备管理办法,计算机设备管理实施办法

热门文章

  1. 高级php面试题(转)
  2. 一幅图看懂prototype与[[Prototype]]
  3. [转]Unix awk完全使用手册
  4. CodeForces - 1514B AND 0, Sum Big【快速模幂】
  5. 2016CCPC东北地区大学生程序设计竞赛题解
  6. UVA10624 Super Number【DFS】
  7. ACM程序设计基础(1)题解
  8. CCF201703试题
  9. 51Nod-1126 求递推序列的第N项【递推序列+模除】
  10. CCF201403-1 相反数(解法二)(100分)(废除!!!)