自定义年月日显示框
//
// JackDateAndDateView.m
// ZHB
//
// Created by JackRen on 16/5/10.
// Copyright © 2016年 atense. All rights reserved.
//
#import "JackDateAndDateView.h"
#import "UIView+Common.h"
#define NUMBERS @"0123456789"
#define BUTTONWIDTH 61
#define MARGINWIDTH 10
#define FIRSTVIEWWIDTH 95
@interface JackDateAndDateView()
@end
@implementation JackDateAndDateView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.titleLabel];
[self addSubview:self.firstLabel];
[self addSubview:self.firstView];
[self.firstView addSubview:self.firstAddButton];
[self.firstView addSubview:self.firstTextFiled];
[self.firstView addSubview:self.firstMouseButton];
[self addSubview:self.secondView];
[self.secondView addSubview:self.secondAddButton];
[self.secondView addSubview:self.secondTextFiled];
[self.secondView addSubview:self.secondMouseButton];
[self addSubview:self.threeView];
[self.threeView addSubview:self.threeAddButton];
[self.threeView addSubview:self.threeTextFiled];
[self.threeView addSubview:self.threeMouseButton];
[self addSubview:self.secondLabel];
[self addSubview:self.cancleButton];
[self addSubview:self.ensureButton];
}
return self;
}
/**
* 标题的初始化
*
* @return label
*/
- (UILabel *)titleLabel {
if (!_titleLabel) {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, MARGINWIDTH*KScDefineWidth, width(self.frame), 30*KScDefineWidth)];
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
_titleLabel.text = [NSString stringWithFormat:@"%@年-%@月-%@日 %@",ary[0],ary[1],ary[2],[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:strUrl]]];
_titleLabel.font = [UIFont systemFontOfSize:20*KScDefineWidth];
_titleLabel.textColor = LHColor(69, 69, 69);
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
/**
* 标题下方的一条线
*
* @return label
*/
- (UILabel *)firstLabel {
if (!_firstLabel) {
self.firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, maxY(self.titleLabel) + MARGINWIDTH*KScDefineWidth, width(self.frame), 2*KScDefineWidth)];
_firstLabel.backgroundColor = LHColor(25, 158 , 253);
}
return _firstLabel;
}
/**
* 下方的一条线
*
* @return label
*/
- (UILabel *)secondLabel {
if (!_secondLabel) {
self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, minY(self.cancleButton) - MARGINWIDTH*KScDefineWidth, width(self.frame), 2*KScDefineWidth)];
_secondLabel.backgroundColor = LHColor(25, 158 , 253);
}
return _secondLabel;
}
/**
* 确定按钮的实现
*
* @return button
*/
- (UIButton *)ensureButton {
if (!_ensureButton) {
self.ensureButton = [UIButton buttonWithType:UIButtonTypeCustom];
_ensureButton.frame = CGRectMake(maxX(_cancleButton) + 10*KScDefineWidth, minY(_cancleButton),width(_cancleButton.frame) , height(self.cancleButton.frame));
_ensureButton.backgroundColor = LHColor(25, 158 , 253);
_ensureButton.layer.cornerRadius = 4.0;
_ensureButton.layer.borderWidth = 0.5*KScDefineWidth;
_ensureButton.layer.borderColor = [LINEBORDERCOLOR CGColor];
[_ensureButton setTitle:@"确定" forState:UIControlStateNormal];
[_ensureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
return _ensureButton;
}
/**
* 取消按钮的实现
*
* @return button
*/
- (UIButton *)cancleButton {
if (!_cancleButton) {
self.cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_cancleButton.frame = CGRectMake(10*KScDefineWidth, height(self.frame) - 70*KScDefineWidth,(width(self.frame) - 30*KScDefineWidth)/2 , 60*KScDefineWidth);
_cancleButton.backgroundColor = LHColor(25, 158 , 253);
_cancleButton.layer.cornerRadius = 4.0;
_cancleButton.layer.borderWidth = 0.5*KScDefineWidth;
_cancleButton.layer.borderColor = [LINEBORDERCOLOR CGColor];
[_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
return _cancleButton;
}
/**
* 创建第一块时间块
*
* @return view
*/
#pragma mark --
#pragma mark 创建第一个时间块
- (UIView *)firstView {
if (!_firstView) {
self.firstView = [[UIView alloc] initWithFrame:CGRectMake((width(self.frame) - BUTTONWIDTH*2*KScDefineWidth - FIRSTVIEWWIDTH*KScDefineWidth - MARGINWIDTH*KScDefineWidth*2)/2, maxY(self.firstLabel) + (height(self.frame) - height(self.titleLabel.frame) - height(self.cancleButton.frame) - 4*MARGINWIDTH*KScDefineWidth - 3*BUTTONWIDTH*KScDefineWidth)/2, FIRSTVIEWWIDTH*KScDefineWidth, 3*BUTTONWIDTH*KScDefineWidth)];
_firstView.layer.cornerRadius = 4.0;
_firstView.layer.borderColor = [[UIColor blackColor] CGColor];
_firstView.layer.borderWidth = 0.5*KScDefineWidth;
}
return _firstView;
}
- (UIButton *)firstAddButton {
if (!_firstAddButton) {
self.firstAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
_firstAddButton.frame = CGRectMake(17*KScDefineHeight,0 , BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_firstAddButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _firstAddButton.bounds;
maskLayer.path = maskPath.CGPath;
_firstAddButton.layer.mask = maskLayer;
[_firstAddButton setBackgroundImage:[UIImage imageNamed:@"img_add_black"] forState:UIControlStateNormal];
[_firstAddButton addTarget:self action:@selector(firstAddButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _firstAddButton;
}
#pragma mark --第一个加号的点击事件
- (void)firstAddButtonAction:(UIButton *)firstAction {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if ([self.firstTextFiled.text integerValue] < [ary[0] integerValue]) {
self.firstTextFiled.text = [NSString stringWithFormat:@"%@年",ary[0]];
}else {
NSInteger year = [self.firstTextFiled.text integerValue];
year++;
NSString *str = [NSString stringWithFormat:@"%ld",year];
self.firstTextFiled.text = [NSString stringWithFormat:@"%@年",str];
}
//TODO:根据输入的年份判断当前的天数,进而改变日期
NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
if (days < [self.threeTextFiled.text integerValue]) {
self.threeTextFiled.text = [NSString stringWithFormat:@"01日"];
}
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
- (UITextField *)firstTextFiled {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if (!_firstTextFiled) {
self.firstTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, maxY(self.firstAddButton), FIRSTVIEWWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth)];
_firstTextFiled.text = [NSString stringWithFormat:@"%@年",ary[0]];;
_firstTextFiled.textColor = LHColor(108, 108, 108);
_firstTextFiled.font = [UIFont systemFontOfSize:26*KScDefineWidth];
_firstTextFiled.textAlignment = NSTextAlignmentCenter;
_firstTextFiled.layer.borderColor = [[UIColor blackColor] CGColor];
_firstTextFiled.layer.borderWidth = 0.5*KScDefineWidth;
_firstTextFiled.enabled = NO;
_firstTextFiled.delegate = self;
[_firstTextFiled addTarget:self action:@selector(firstTextFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
}
return _firstTextFiled;
}
- (void)firstTextFieldEditChanged:(UITextField *)textField {
if (textField.text.length >= 3) {
self.firstTextFiled.text = [NSString stringWithFormat:@"%@",[textField.text substringToIndex:2]];
}else if (textField.text.length == 2){
if ([textField.text integerValue] > 23) {
self.firstTextFiled.text = @"08";
}
}
}
- (UIButton *)firstMouseButton {
if (!_firstMouseButton) {
self.firstMouseButton = [UIButton buttonWithType:UIButtonTypeCustom];
_firstMouseButton.frame = CGRectMake(17*KScDefineWidth, maxY(self.firstTextFiled), BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_firstMouseButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _firstMouseButton.bounds;
maskLayer.path = maskPath.CGPath;
_firstMouseButton.layer.mask = maskLayer;
[_firstMouseButton setBackgroundImage:[UIImage imageNamed:@"img_minnus_black"] forState:UIControlStateNormal];
[_firstMouseButton addTarget:self action:@selector(firstMouseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _firstMouseButton;
}
#pragma mark --第一个减号的点击事件
- (void)firstMouseButtonAction:(UIButton *)firstMouse {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if ([self.firstTextFiled.text integerValue] <= [ary[0] integerValue]) {
self.firstTextFiled.text = [NSString stringWithFormat:@"%@年",ary[0]];
}else {
NSInteger hour = [self.firstTextFiled.text integerValue];
hour--;
NSString *str = [NSString stringWithFormat:@"%ld",hour];
self.firstTextFiled.text = [NSString stringWithFormat:@"%@年",str];
}
//TODO:根据输入的年份判断当前的天数,进而改变日期
NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
if (days < [self.threeTextFiled.text integerValue]) {
self.threeTextFiled.text = [NSString stringWithFormat:@"01日"];
}
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
/**
* 创建第二个时间块
*
* @return view
*/
#pragma mark -
#pragma mark 创建第二个时间控制块
- (UIView *)secondView {
if (!_secondView) {
self.secondView = [[UIView alloc] initWithFrame:CGRectMake(maxX(self.firstView) + MARGINWIDTH*KScDefineWidth, minY(self.firstView), BUTTONWIDTH*KScDefineWidth, 3*BUTTONWIDTH*KScDefineWidth)];
_secondView.layer.cornerRadius = 4.0;
_secondView.layer.borderColor = [[UIColor blackColor] CGColor];
_secondView.layer.borderWidth = 0.5*KScDefineWidth;
}
return _secondView;
}
- (UIButton *)secondAddButton {
if (!_secondAddButton) {
self.secondAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
_secondAddButton.frame = CGRectMake( 0,0 , BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_secondAddButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _secondAddButton.bounds;
maskLayer.path = maskPath.CGPath;
_secondAddButton.layer.mask = maskLayer;
[_secondAddButton setBackgroundImage:[UIImage imageNamed:@"img_add_black"] forState:UIControlStateNormal];
[_secondAddButton addTarget:self action:@selector(secondAddButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _secondAddButton;
}
#pragma mark --第二个加号的点击事件
- (void)secondAddButtonAction:(UIButton *)secondAction {
if ([self.secondTextFiled.text integerValue] == 12) {
self.secondTextFiled.text = @"01月";
self.firstTextFiled.text = [NSString stringWithFormat:@"%ld年",[self.firstTextFiled.text integerValue] + 1];
}else {
NSInteger month = [self.secondTextFiled.text integerValue];
month++;
NSString *str = [NSString stringWithFormat:@"%ld",month];
if (str.length == 1) {
self.secondTextFiled.text = [NSString stringWithFormat:@"0%@月",str];
}else {
self.secondTextFiled.text = [NSString stringWithFormat:@"%@月",str];
}
}
//TODO:根据输入的月份判断当前的天数,进而改变日期
NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
if (days < [self.threeTextFiled.text integerValue]) {
self.threeTextFiled.text = [NSString stringWithFormat:@"01日"];
}
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
- (UITextField *)secondTextFiled {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if (!_secondTextFiled) {
self.secondTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(minX(self.secondAddButton), maxY(self.secondAddButton), BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth)];
_secondTextFiled.text = [NSString stringWithFormat:@"%@月",ary[1]];
_secondTextFiled.textColor = LHColor(108, 108, 108);
_secondTextFiled.font = [UIFont systemFontOfSize:26*KScDefineWidth];
_secondTextFiled.textAlignment = NSTextAlignmentCenter;
_secondTextFiled.layer.borderColor = [[UIColor blackColor] CGColor];
_secondTextFiled.layer.borderWidth = 0.5*KScDefineWidth;
_secondTextFiled.enabled = NO;
_secondTextFiled.delegate = self;
[_secondTextFiled addTarget:self action:@selector(secondTextFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
}
return _secondTextFiled;
}
- (void)secondTextFieldEditChanged:(UITextField *)textField {
if (textField.text.length >= 3) {
self.secondTextFiled.text = [NSString stringWithFormat:@"%@",[textField.text substringToIndex:2]];
}else if (textField.text.length == 2){
if ([textField.text integerValue] > 59) {
self.secondTextFiled.text = @"00";
}
}
}
- (UIButton *)secondMouseButton {
if (!_secondMouseButton) {
self.secondMouseButton = [UIButton buttonWithType:UIButtonTypeCustom];
_secondMouseButton.frame = CGRectMake(minX(self.secondTextFiled), maxY(self.secondTextFiled), BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_secondMouseButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _secondMouseButton.bounds;
maskLayer.path = maskPath.CGPath;
_secondMouseButton.layer.mask = maskLayer;
[_secondMouseButton setBackgroundImage:[UIImage imageNamed:@"img_minnus_black"] forState:UIControlStateNormal];
[_secondMouseButton addTarget:self action:@selector(secondMouseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _secondMouseButton;
}
#pragma mark --第二个减号的点击事件
- (void)secondMouseButtonAction:(UIButton *)firstMouse {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if ([self.secondTextFiled.text integerValue] == 1) {
self.secondTextFiled.text = @"12月";
//TODO:只有当输入的年份大于当前的年份时,改变月份才能影响年,否则无效
if ([self.firstTextFiled.text integerValue] > [ary[0] integerValue]) {
self.firstTextFiled.text = [NSString stringWithFormat:@"%ld年",[self.firstTextFiled.text integerValue] - 1];
}
}else {
NSInteger minute = [self.secondTextFiled.text integerValue];
minute--;
NSString *str = [NSString stringWithFormat:@"%ld",minute];
if (str.length == 1) {
self.secondTextFiled.text = [NSString stringWithFormat:@"0%@月",str];
}else {
self.secondTextFiled.text = [NSString stringWithFormat:@"%@月",str];
}
}
//TODO:根据输入的月份判断当前的天数,进而改变日期
NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
if (days < [self.threeTextFiled.text integerValue]) {
self.threeTextFiled.text = [NSString stringWithFormat:@"01日"];
}
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
/**
* 创建第三个时间块
*
* @return view
*/
#pragma mark -
#pragma mark 创建第三个时间控制块
- (UIView *)threeView {
if (!_threeView) {
self.threeView = [[UIView alloc] initWithFrame:CGRectMake(maxX(self.secondView) +MARGINWIDTH*KScDefineWidth, minY(self.secondView), BUTTONWIDTH*KScDefineWidth, 3*BUTTONWIDTH*KScDefineWidth)];
_threeView.layer.cornerRadius = 4.0;
_threeView.layer.borderColor = [[UIColor blackColor] CGColor];
_threeView.layer.borderWidth = 0.5*KScDefineWidth;
}
return _threeView;
}
- (UIButton *)threeAddButton {
if (!_threeAddButton) {
self.threeAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
_threeAddButton.frame = CGRectMake( 0,0 , BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_threeAddButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _threeAddButton.bounds;
maskLayer.path = maskPath.CGPath;
_threeAddButton.layer.mask = maskLayer;
[_threeAddButton setBackgroundImage:[UIImage imageNamed:@"img_add_black"] forState:UIControlStateNormal];
[_threeAddButton addTarget:self action:@selector(threeAddButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _threeAddButton;
}
#pragma mark --第三个加号的点击事件
- (void)threeAddButtonAction:(UIButton *)threeAction {
NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
if ([self.threeTextFiled.text integerValue] == days) {
self.threeTextFiled.text = @"01日";
if ([self.secondTextFiled.text integerValue] == 12) {
self.secondTextFiled.text = @"01月";
self.firstTextFiled.text = [NSString stringWithFormat:@"%ld",[self.firstTextFiled.text integerValue] + 1];
}else {
NSInteger month = [self.secondTextFiled.text integerValue] + 1;
NSString *str = [NSString stringWithFormat:@"%ld",month];
if (str.length == 1) {
self.secondTextFiled.text = [NSString stringWithFormat:@"0%@月",str];
}else {
self.secondTextFiled.text = [NSString stringWithFormat:@"%@月",str];
}
}
}else {
NSInteger hour = [self.threeTextFiled.text integerValue];
hour++;
NSString *str = [NSString stringWithFormat:@"%ld",hour];
if (str.length == 1) {
self.threeTextFiled.text = [NSString stringWithFormat:@"0%@日",str];
}else {
self.threeTextFiled.text = [NSString stringWithFormat:@"%@日",str];
}
}
//TODO:根据输入的月份判断当前的天数,进而改变日期
// NSInteger days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] withYearDays:[self.firstTextFiled.text integerValue]];
// if (days < [self.threeTextFiled.text integerValue]) {
// self.threeTextFiled.text = [NSString stringWithFormat:@"01日"];
// }
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
- (UITextField *)threeTextFiled {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
if (!_threeTextFiled) {
self.threeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(minX(self.threeAddButton), maxY(self.threeAddButton), BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth)];
_threeTextFiled.text = [NSString stringWithFormat:@"%@日",ary[2]];
_threeTextFiled.textColor = LHColor(108, 108, 108);
_threeTextFiled.font = [UIFont systemFontOfSize:26*KScDefineWidth];
_threeTextFiled.textAlignment = NSTextAlignmentCenter;
_threeTextFiled.layer.borderColor = [[UIColor blackColor] CGColor];
_threeTextFiled.layer.borderWidth = 0.5*KScDefineWidth;
_threeTextFiled.enabled = NO;
_threeTextFiled.delegate = self;
[_threeTextFiled addTarget:self action:@selector(threeTextFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
}
return _threeTextFiled;
}
- (void)threeTextFieldEditChanged:(UITextField *)textField {
if (textField.text.length >= 3) {
self.threeTextFiled.text = [NSString stringWithFormat:@"%@",[textField.text substringToIndex:2]];
}else if (textField.text.length == 2){
if ([textField.text integerValue] > 23) {
self.threeTextFiled.text = @"18";
}
}
}
- (UIButton *)threeMouseButton {
if (!_threeMouseButton) {
self.threeMouseButton = [UIButton buttonWithType:UIButtonTypeCustom];
_threeMouseButton.frame = CGRectMake(minX(self.threeTextFiled), maxY(self.threeTextFiled), BUTTONWIDTH*KScDefineWidth, BUTTONWIDTH*KScDefineWidth);
//TODO:uiview 单边圆角或者单边框
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_threeMouseButton.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5,5)];//圆角大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _threeMouseButton.bounds;
maskLayer.path = maskPath.CGPath;
_threeMouseButton.layer.mask = maskLayer;
[_threeMouseButton setBackgroundImage:[UIImage imageNamed:@"img_minnus_black"] forState:UIControlStateNormal];
[_threeMouseButton addTarget:self action:@selector(threeMouseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _threeMouseButton;
}
#pragma mark --第三个减号的点击事件
- (void)threeMouseButtonAction:(UIButton *)threeMouse {
NSString *str = [self getCurrentDate];
NSArray *ary = [str componentsSeparatedByString:@"-"];
NSInteger days = 0;
if ([self.threeTextFiled.text integerValue] == 1) {
if ([self.secondTextFiled.text integerValue] == 1) {
days = [self getCurrantMonthDays:12 withYearDays:[self.firstTextFiled.text integerValue]];
}else {
days = [self getCurrantMonthDays:[self.secondTextFiled.text integerValue] - 1 withYearDays:[self.firstTextFiled.text integerValue]];
}
self.threeTextFiled.text = [NSString stringWithFormat:@"%ld日",days];
if ([self.secondTextFiled.text integerValue] == 1) {
self.secondTextFiled.text = @"12月";
if ([self.firstTextFiled.text integerValue] > [ary[0] integerValue]) {
self.firstTextFiled.text = [NSString stringWithFormat:@"%ld年",[self.firstTextFiled.text integerValue] - 1];
}
}else {
NSInteger month = [self.secondTextFiled.text integerValue] - 1;
NSString *str = [NSString stringWithFormat:@"%ld",month];
if (str.length == 1) {
self.secondTextFiled.text = [NSString stringWithFormat:@"0%@月",str];
}else {
self.secondTextFiled.text = [NSString stringWithFormat:@"%@月",str];
}
}
}else {
NSInteger hour = [self.threeTextFiled.text integerValue];
hour--;
NSString *str = [NSString stringWithFormat:@"%ld",hour];
if (str.length == 1) {
self.threeTextFiled.text = [NSString stringWithFormat:@"0%@日",str];
}else {
self.threeTextFiled.text = [NSString stringWithFormat:@"%@日",str];
}
}
NSString *dateStr = [NSString stringWithFormat:@"%ld%@%@",[self.firstTextFiled.text integerValue],[self.secondTextFiled.text substringToIndex:2],[self.threeTextFiled.text substringToIndex:2]];
self.titleLabel.text = [NSString stringWithFormat:@"%@-%@-%@ %@",self.firstTextFiled.text,self.secondTextFiled.text,self.threeTextFiled.text,[JackDateAndDateView weekdayStringFromDate:[self getCurrentDate:dateStr]]];
}
#pragma mark 计算星期几
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
#pragma mark -
#pragma mark 获取当前的日期
- (NSString *)getCurrentDate {
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd"];
NSString *locationString=[dateformatter stringFromDate:senddate];
return locationString;
}
#pragma mark -
#pragma mark 字符串转换为日期类型
- (NSDate *)getCurrentDate:(NSString *)string {
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"Asia/Shanghai"]];
[inputFormatter setDateFormat:@"yyyyMMdd"];
NSDate* inputDate = [inputFormatter dateFromString:string];
return inputDate;
}
#pragma mark -
#pragma mark 获取当前天数
- (NSInteger)getCurrantMonthDays:(NSInteger)month withYearDays:(NSInteger)year {
NSArray *ary = @[@"31",@"28",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31"];
if (2 == month && 0 == (year % 4) && (0 != (year % 100) || 0 == (year % 400))) {
return [ary[1] integerValue] + 1;
}
return [ary[month - 1] integerValue];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end