自定义年月日显示框

图片发自简书App

//

//  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容