iOS自定义年月日

//
//  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()<UITextFieldDelegate>

@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
//
//  JackDateAndDateView.h
//  ZHB
//
//  Created by JackRen on 16/5/10.
//  Copyright © 2016年 atense. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface JackDateAndDateView : UIView

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *firstLabel;

@property (nonatomic,strong) UIView *firstView;
@property (nonatomic,strong) UIView *secondView;
@property (nonatomic,strong) UIView *threeView;
@property (nonatomic,strong) UIView *fourView;

@property (nonatomic,strong) UIButton *firstAddButton;
@property (nonatomic,strong) UIButton *firstMouseButton;
@property (nonatomic,strong) UITextField *firstTextFiled;

@property (nonatomic,strong) UIButton *secondAddButton;
@property (nonatomic,strong) UIButton *secondMouseButton;
@property (nonatomic,strong) UITextField *secondTextFiled;

@property (nonatomic,strong) UIButton *threeAddButton;
@property (nonatomic,strong) UIButton *threeMouseButton;
@property (nonatomic,strong) UITextField *threeTextFiled;

@property (nonatomic,strong) UILabel *secondLabel;
@property (nonatomic,strong) UIButton *cancleButton;
@property (nonatomic,strong) UIButton *ensureButton;

@end

时间: 2024-08-14 07:49:21

iOS自定义年月日的相关文章

IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果

拖个button继承一下  几行代码 就搞定 自用效果还行 IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果,布布扣,bubuko.com

iOS 自定义页面的切换动画与交互动画

在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖它.但是随着iOS7的到来,Apple针对开发者推出了新的工具,以更灵活地方式管理UIViewController切换. 自定义导航栏的Push/Pop动画 为了在基于UINavigationController下做自定义的动画切换,先建立一个简单的工程,这个工程的rootViewController是一个

iOS自定义转场动画实战讲解

iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar  的解决办法 问题:iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 1.自定义代码: - (void)viewWillAppear:(BOOL)animated { [super  viewWillAppear:animated]; // 删除系统自动生成的UITabBarB

IOS 自定义 滑动返回 手势

/** 只需要在你自定义的导航控制器中,改成如下代码即可,自定义手势返回 */ #define KEY_WINDOW [[UIApplication sharedApplication] keyWindow] #define kScreenW KEY_WINDOW.bounds.size.width #define kAnimateDuration 0.3 #define kDefaultAlapa 0.5 #define kDefaultScale 0.95 #define iOS7 ([UI

IOS开发—IOS自定义任意位置右滑POP视图控制器

IOS自定义任意位置右滑POP视图控制器 IOS7.0之后系统提供了原生的从左边缘滑动pop出栈的方法,也可以自定义左边缘pop出栈,将在下一篇介绍,本篇介绍通过添加手势的方法实现IOS当前屏幕任意位置(非指定左边缘)右滑pop视图控制器出栈.代码如下: // // LXXPopViewController.m // 任意点右滑Pop // // Created by Lotheve on 15/6/12. // Copyright (c) 2015年Lotheve. All rights re

iOS自定义的UISwitch按钮

UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc]initWithFrame:CGRectMake(200.0,10.0,0.0,0.0)]; 是不是很奇怪,大小竟然是0.0×0.0,没错,系统会自动帮你决定最佳的尺寸,你自己写的尺寸会被忽略掉,你只要定义好相对父视图的位置就好了.默认尺寸为79 * 27. 二.显示控件 [ parrentView

iOS 自定义TabBarController zhuanzai

iOS 自定义TabBarController 一.自定义的思路 iOS中的TabBarController确实已经很强大了,大部分主流iOS应用都会采用.但是往往也不能满足全部的需求,因此需要自定义TabBar,自定义需要对系统的TabBar工作方式有很好的理解,自定义需要勇气. 自定义TabBar的原则:尽量利用系统自带TabBar,只改需要改的地方. 二.自定义TabBar的总体过程 1.先把自带的TabBar条给取消了 2.自己做一个view,上面放几个按钮,设定按钮的点击事件.并设置s

iOS自定义segue和自定义segue转场动画

自定义segue需要继承 UIStoryboardSegue类,然后重写-(void)perform;方法.在方法中可以自定义转场动画. </pre><span style="color: rgb(97, 34, 174); font-family: Courier; font-size: 18px;">CoolSegue.h文件</span><p></p><p></p><pre code_sni