// // CCDatePickerView.m // ChooseTimePickerView // // Created by 陈红 on 2017/3/12. // Copyright © 2017年 陈红:ch_email@126.cm. All rights reserved. // #import "CCDatePickerView.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height #define H(height) ScreenHeight*(height)/1136 #define W(width) ScreenWidth*(width)/640 #define Width 640 #define Height 1136 #define kPickerSize self.datePicker.frame.size #define RGBA(r, g, b, a) ([UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:a]) #define MAXYEAR 2050 #define MINYEAR 1970 @interface CCDatePickerView () { //日期存储数组 NSMutableArray *_yearArray; NSMutableArray *_monthArray; NSMutableArray *_dayArray; NSMutableArray *_hourArray; NSMutableArray *_minuteArray; //记录日期位置 NSInteger yearIndex; NSInteger monthIndex; NSInteger dayIndex; NSInteger hourIndex; NSInteger minuteIndex; //记录选择日期 NSDate *_startDate; } @property(nonatomic,strong)UIView *contentView; @property(nonatomic,strong)UIView *dateView; @property(nonatomic,strong)UILabel *yearLabel; @property(nonatomic,strong)UILabel *monthLabel; @property(nonatomic,strong)UILabel *dayLabel; @property(nonatomic,strong)UILabel *startHourLabel; @property(nonatomic,strong)UILabel *endLabel; @property(nonatomic,strong)UIView *line1; @property(nonatomic,strong)UIView *line2; @property(nonatomic,strong)UIView *line3; @property(nonatomic,strong)UIView *line4; @property (nonatomic, copy)NSString *dateFormatter;//设置日期格式 @property (nonatomic, retain)NSDate *scrollToDate;//滚到指定日期 @end @implementation CCDatePickerView - (instancetype)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { _dateFormatter = @"yyyy-MM-dd HH:mm"; [self setupUI]; [self defaultConfig]; [self getNowDate:nil animated:NO];//当前时间 } return self; } -(void)setupUI { UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]; [self addSubview:contentView]; self.contentView = contentView; self.dateView=[[UIView alloc] initWithFrame:CGRectMake(W(30), 64+50, ScreenWidth-W(60), H(550))]; self.dateView.backgroundColor=[UIColor whiteColor]; self.dateView.layer.cornerRadius=8; [self.dateView.layer setMasksToBounds:YES]; [contentView addSubview:self.dateView]; self.dateView.userInteractionEnabled=YES; self.chooseTimeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.dateView.size.width, H(80))]; self.chooseTimeLabel.text=@"请选择服务时间"; self.chooseTimeLabel.textColor=[UIColor grayColor]; self.chooseTimeLabel.font=[UIFont systemFontOfSize:16]; self.chooseTimeLabel.textAlignment=NSTextAlignmentCenter; [self.dateView addSubview:self.chooseTimeLabel]; self.line1=[[UIView alloc] initWithFrame:CGRectMake(0, self.chooseTimeLabel.bottom, self.dateView.size.width, 1)]; self.line1.backgroundColor= RGBA(237, 237, 237, 1.0); [self.dateView addSubview:self.line1]; [self addLabelTextWithName:@[@"年",@"月",@"日",@"始",@"束"]]; self.line2=[[UIView alloc] initWithFrame:CGRectMake(0, self.yearLabel.bottom, self.dateView.size.width, 1)]; self.line2.backgroundColor=RGBA(237, 237, 237, 1.0); [self.dateView addSubview:self.line2]; self.line3=[[UIView alloc] initWithFrame:CGRectMake(self.dateView.size.width*4/7, self.line1.bottom, 1, self.dateView.size.height-H(160))]; self.line3.backgroundColor=RGBA(237, 237, 237, 1.0); [self.dateView addSubview:self.line3]; self.line4=[[UIView alloc] initWithFrame:CGRectMake(0, self.line3.bottom, self.dateView.size.width, 1)]; self.line4.backgroundColor=RGBA(237, 237, 237, 1.0); [self.dateView addSubview:self.line4]; self.cancelButton=[UIButton buttonWithType:UIButtonTypeSystem]; [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [self.cancelButton setTintColor:[UIColor grayColor]]; self.cancelButton.titleLabel.font=[UIFont systemFontOfSize:14]; self.cancelButton.frame=CGRectMake(0, self.line4.bottom, self.dateView.size.width*4/7, H(80)); self.cancelButton.tag = 100; [self.cancelButton addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside]; [self.dateView addSubview:self.cancelButton]; self.okButton=[UIButton buttonWithType:UIButtonTypeSystem]; [self.okButton setTitle:@"确定" forState:UIControlStateNormal]; [self.okButton setTintColor:RGBA(210, 55, 61, 1.0)]; self.okButton.titleLabel.font=[UIFont systemFontOfSize:14]; self.okButton.frame=CGRectMake(self.cancelButton.right, self.line4.bottom, self.dateView.size.width*3/7, H(80)); [self.dateView addSubview:self.okButton]; self.okButton.tag = 101; [self.okButton addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside]; [self.dateView addSubview:self.datePicker]; } -(void)defaultConfig { if (!_scrollToDate) { _scrollToDate = [NSDate date]; } //设置年月日时分数据 _yearArray = [self setArray:_yearArray]; _monthArray = [self setArray:_monthArray]; _dayArray = [self setArray:_dayArray]; _hourArray = [self setArray:_hourArray]; _minuteArray = [self setArray:_minuteArray]; for (int i=0; i<24; i++) { NSString *num = [NSString stringWithFormat:@"%02d",i]; if (0 *)touches withEvent:(UIEvent *)event{ [self fadeOut]; } @end static const unsigned componentFlags = (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal); @implementation NSDate (Extension) // Courtesy of Lukasz Margielewski // Updated via Holger Haenisch + (NSCalendar *) currentCalendar { static NSCalendar *sharedCalendar = nil; if (!sharedCalendar) sharedCalendar = [NSCalendar autoupdatingCurrentCalendar]; return sharedCalendar; } #pragma mark - Relative Dates + (NSDate *) dateWithDaysFromNow: (NSInteger) days { // Thanks, Jim Morrison return [[NSDate date] dateByAddingDays:days]; } + (NSDate *) dateWithDaysBeforeNow: (NSInteger) days { // Thanks, Jim Morrison return [[NSDate date] dateBySubtractingDays:days]; } + (NSDate *) dateTomorrow { return [NSDate dateWithDaysFromNow:1]; } + (NSDate *) dateYesterday { return [NSDate dateWithDaysBeforeNow:1]; } + (NSDate *) dateWithHoursFromNow: (NSInteger) dHours { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_HOUR * dHours; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } + (NSDate *) dateWithHoursBeforeNow: (NSInteger) dHours { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_HOUR * dHours; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } + (NSDate *) dateWithMinutesFromNow: (NSInteger) dMinutes { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_MINUTE * dMinutes; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } #pragma mark - String Properties - (NSString *) stringWithFormat: (NSString *) format { NSDateFormatter *formatter = [NSDateFormatter new]; // formatter.locale = [NSLocale currentLocale]; // Necessary? formatter.dateFormat = format; return [formatter stringFromDate:self]; } - (NSString *) stringWithDateStyle: (NSDateFormatterStyle) dateStyle timeStyle: (NSDateFormatterStyle) timeStyle { NSDateFormatter *formatter = [NSDateFormatter new]; formatter.dateStyle = dateStyle; formatter.timeStyle = timeStyle; // formatter.locale = [NSLocale currentLocale]; // Necessary? return [formatter stringFromDate:self]; } - (NSString *) shortString { return [self stringWithDateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]; } - (NSString *) shortTimeString { return [self stringWithDateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle]; } - (NSString *) shortDateString { return [self stringWithDateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]; } - (NSString *) mediumString { return [self stringWithDateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle ]; } - (NSString *) mediumTimeString { return [self stringWithDateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterMediumStyle ]; } - (NSString *) mediumDateString { return [self stringWithDateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]; } - (NSString *) longString { return [self stringWithDateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterLongStyle ]; } - (NSString *) longTimeString { return [self stringWithDateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterLongStyle ]; } - (NSString *) longDateString { return [self stringWithDateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle]; } #pragma mark - Comparing Dates - (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate { NSDateComponents *components1 = [[NSDate currentCalendar] components:componentFlags fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:componentFlags fromDate:aDate]; return ((components1.year == components2.year) && (components1.month == components2.month) && (components1.day == components2.day)); } - (BOOL) isToday { return [self isEqualToDateIgnoringTime:[NSDate date]]; } - (BOOL) isTomorrow { return [self isEqualToDateIgnoringTime:[NSDate dateTomorrow]]; } - (BOOL) isYesterday { return [self isEqualToDateIgnoringTime:[NSDate dateYesterday]]; } // This hard codes the assumption that a week is 7 days - (BOOL) isSameWeekAsDate: (NSDate *) aDate { NSDateComponents *components1 = [[NSDate currentCalendar] components:componentFlags fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:componentFlags fromDate:aDate]; // Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week if (components1.weekOfMonth != components2.weekOfMonth) return NO; // Must have a time interval under 1 week. Thanks @aclark return (fabs([self timeIntervalSinceDate:aDate]) < D_WEEK); } - (BOOL) isThisWeek { return [self isSameWeekAsDate:[NSDate date]]; } - (BOOL) isNextWeek { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_WEEK; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return [self isSameWeekAsDate:newDate]; } - (BOOL) isLastWeek { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_WEEK; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return [self isSameWeekAsDate:newDate]; } // Thanks, mspasov - (BOOL) isSameMonthAsDate: (NSDate *) aDate { NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:aDate]; return ((components1.month == components2.month) && (components1.year == components2.year)); } - (BOOL) isThisMonth { return [self isSameMonthAsDate:[NSDate date]]; } // Thanks Marcin Krzyzanowski, also for adding/subtracting years and months - (BOOL) isLastMonth { return [self isSameMonthAsDate:[[NSDate date] dateBySubtractingMonths:1]]; } - (BOOL) isNextMonth { return [self isSameMonthAsDate:[[NSDate date] dateByAddingMonths:1]]; } - (BOOL) isSameYearAsDate: (NSDate *) aDate { NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:aDate]; return (components1.year == components2.year); } - (BOOL) isThisYear { // Thanks, baspellis return [self isSameYearAsDate:[NSDate date]]; } - (BOOL) isNextYear { NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]]; return (components1.year == (components2.year + 1)); } - (BOOL) isLastYear { NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self]; NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]]; return (components1.year == (components2.year - 1)); } - (BOOL) isEarlierThanDate: (NSDate *) aDate { return ([self compare:aDate] == NSOrderedAscending); } - (BOOL) isLaterThanDate: (NSDate *) aDate { return ([self compare:aDate] == NSOrderedDescending); } // Thanks, markrickert - (BOOL) isInFuture { return ([self isLaterThanDate:[NSDate date]]); } // Thanks, markrickert - (BOOL) isInPast { return ([self isEarlierThanDate:[NSDate date]]); } #pragma mark - Roles - (BOOL) isTypicallyWeekend { NSDateComponents *components = [[NSDate currentCalendar] components:NSCalendarUnitWeekday fromDate:self]; if ((components.weekday == 1) || (components.weekday == 7)) return YES; return NO; } - (BOOL) isTypicallyWorkday { return ![self isTypicallyWeekend]; } #pragma mark - Adjusting Dates // Thaks, rsjohnson - (NSDate *) dateByAddingYears: (NSInteger) dYears { NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setYear:dYears]; NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0]; return newDate; } - (NSDate *) dateBySubtractingYears: (NSInteger) dYears { return [self dateByAddingYears:-dYears]; } - (NSDate *) dateByAddingMonths: (NSInteger) dMonths { NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setMonth:dMonths]; NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0]; return newDate; } - (NSDate *) dateBySubtractingMonths: (NSInteger) dMonths { return [self dateByAddingMonths:-dMonths]; } // Courtesy of dedan who mentions issues with Daylight Savings - (NSDate *) dateByAddingDays: (NSInteger) dDays { NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setDay:dDays]; NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0]; return newDate; } - (NSDate *) dateBySubtractingDays: (NSInteger) dDays { return [self dateByAddingDays: (dDays * -1)]; } - (NSDate *) dateByAddingHours: (NSInteger) dHours { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_HOUR * dHours; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSDate *) dateBySubtractingHours: (NSInteger) dHours { return [self dateByAddingHours: (dHours * -1)]; } - (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes { NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } - (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes { return [self dateByAddingMinutes: (dMinutes * -1)]; } - (NSDateComponents *) componentsWithOffsetFromDate: (NSDate *) aDate { NSDateComponents *dTime = [[NSDate currentCalendar] components:componentFlags fromDate:aDate toDate:self options:0]; return dTime; } #pragma mark - Extremes - (NSDate *) dateAtStartOfDay { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; components.hour = 0; components.minute = 0; components.second = 0; return [[NSDate currentCalendar] dateFromComponents:components]; } // Thanks gsempe & mteece - (NSDate *) dateAtEndOfDay { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; components.hour = 23; // Thanks Aleksey Kononov components.minute = 59; components.second = 59; return [[NSDate currentCalendar] dateFromComponents:components]; } #pragma mark - Retrieving Intervals - (NSInteger) minutesAfterDate: (NSDate *) aDate { NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; return (NSInteger) (ti / D_MINUTE); } - (NSInteger) minutesBeforeDate: (NSDate *) aDate { NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; return (NSInteger) (ti / D_MINUTE); } - (NSInteger) hoursAfterDate: (NSDate *) aDate { NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; return (NSInteger) (ti / D_HOUR); } - (NSInteger) hoursBeforeDate: (NSDate *) aDate { NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; return (NSInteger) (ti / D_HOUR); } - (NSInteger) daysAfterDate: (NSDate *) aDate { NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; return (NSInteger) (ti / D_DAY); } - (NSInteger) daysBeforeDate: (NSDate *) aDate { NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; return (NSInteger) (ti / D_DAY); } // Thanks, dmitrydims // I have not yet thoroughly tested this - (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate { NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay fromDate:self toDate:anotherDate options:0]; return components.day; } #pragma mark - Decomposing Dates - (NSInteger) nearestHour { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * 30; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; NSDateComponents *components = [[NSDate currentCalendar] components:NSCalendarUnitHour fromDate:newDate]; return components.hour; } - (NSInteger) hour { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.hour; } - (NSInteger) minute { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.minute; } - (NSInteger) seconds { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.second; } - (NSInteger) day { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.day; } - (NSInteger) month { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.month; } - (NSInteger) week { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.weekOfMonth; } - (NSInteger) weekday { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.weekday; } - (NSInteger) nthWeekday // e.g. 2nd Tuesday of the month is 2 { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.weekdayOrdinal; } - (NSInteger) year { NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self]; return components.year; } + (NSDate *)date:(NSString *)datestr WithFormat:(NSString *)format { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[NSLocale currentLocale]]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; [dateFormatter setDateFormat:format]; NSDate *date = [dateFormatter dateFromString:datestr]; #if ! __has_feature(objc_arc) [dateFormatter release]; #endif return date; } - (NSDate *)dateWithYMD { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSString *selfStr = [fmt stringFromDate:self]; return [fmt dateFromString:selfStr]; } -(NSDate *)dateWithFormatter:(NSString *)formatter { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = formatter; NSString *selfStr = [fmt stringFromDate:self]; return [fmt dateFromString:selfStr]; } @end @implementation UIView (Extension) #pragma mark - frame - (void)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } - (void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)x { return self.frame.origin.x; } - (CGFloat)y { return self.frame.origin.y; } - (void)setCenterX:(CGFloat)centerX { CGPoint center = self.center; center.x = centerX; self.center = center; } - (CGFloat)centerX { return self.center.x; } - (void)setCenterY:(CGFloat)centerY{ CGPoint center = self.center; center.y = centerY; self.center = center; } - (CGFloat)centerY { return self.center.y; } - (void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } - (void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } - (CGFloat)height { return self.frame.size.height; } - (CGFloat)width { return self.frame.size.width; } - (void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; } - (CGSize)size { return self.frame.size; } - (void)setOrigin:(CGPoint)origin { CGRect frame = self.frame; frame.origin = origin; self.frame = frame; } - (CGPoint)origin { return self.frame.origin; } - (CGFloat)top { return self.frame.origin.y; } - (void)setTop:(CGFloat)top { CGRect frame = self.frame; frame.origin.y = top; self.frame = frame; } - (CGFloat)left { return self.frame.origin.x; } - (void)setLeft:(CGFloat)left { CGRect frame = self.frame; frame.origin.x = left; self.frame = frame; } - (CGFloat)bottom { return self.frame.size.height + self.frame.origin.y; } - (void)setBottom:(CGFloat)bottom { CGRect frame = self.frame; frame.origin.y = bottom - frame.size.height; self.frame = frame; } - (CGFloat)right { return self.frame.size.width + self.frame.origin.x; } - (void)setRight:(CGFloat)right { CGRect frame = self.frame; frame.origin.x = right - frame.size.width; self.frame = frame; } #pragma mark - layer - (void)rounded:(CGFloat)cornerRadius { [self rounded:cornerRadius width:0 color:nil]; } - (void)border:(CGFloat)borderWidth color:(UIColor *)borderColor { [self rounded:0 width:borderWidth color:borderColor]; } - (void)rounded:(CGFloat)cornerRadius width:(CGFloat)borderWidth color:(UIColor *)borderColor { self.layer.cornerRadius = cornerRadius; self.layer.borderWidth = borderWidth; self.layer.borderColor = [borderColor CGColor]; self.layer.masksToBounds = YES; } -(void)round:(CGFloat)cornerRadius RectCorners:(UIRectCorner)rectCorner { UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } -(void)shadow:(UIColor *)shadowColor opacity:(CGFloat)opacity radius:(CGFloat)radius offset:(CGSize)offset { //给Cell设置阴影效果 self.layer.masksToBounds = NO; self.layer.shadowColor = shadowColor.CGColor; self.layer.shadowOpacity = opacity; self.layer.shadowRadius = radius; self.layer.shadowOffset = offset; } #pragma mark - base - (UIViewController *)viewController { id nextResponder = [self nextResponder]; while (nextResponder != nil) { if ([nextResponder isKindOfClass:[UIViewController class]]) { UIViewController *vc = (UIViewController *)nextResponder; return vc; } nextResponder = [nextResponder nextResponder]; } return nil; } + (CGFloat)getLabelHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)]; label.text = title; label.font = font; label.numberOfLines = 0; [label sizeToFit]; CGFloat height = label.frame.size.height; return height; } @end