THDateDay.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // THDateDay.m
  3. // THCalendarDatePicker
  4. //
  5. // Created by chase wasden on 2/10/13.
  6. // Adapted by Hannes Tribus on 31/07/14.
  7. // Copyright (c) 2014 3Bus. All rights reserved.
  8. //
  9. #import "THDateDay.h"
  10. #import <QuartzCore/QuartzCore.h>
  11. @implementation THDateDay
  12. @synthesize selectedBackgroundColor = _selectedBackgroundColor;
  13. @synthesize currentDateColor = _currentDateColor;
  14. @synthesize currentDateColorSelected = _currentDateColorSelected;
  15. @synthesize rounded = _rounded;
  16. - (id)initWithFrame:(CGRect)frame {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. // Initialization code
  20. _selectedBackgroundColor = [UIColor colorWithRed:89/255.0 green:118/255.0 blue:169/255.0 alpha:1];
  21. _currentDateColor = [UIColor colorWithRed:242/255.0 green:121/255.0 blue:53/255.0 alpha:1.0];
  22. _currentDateColorSelected = [UIColor whiteColor];
  23. _rounded = NO;
  24. }
  25. return self;
  26. }
  27. /*
  28. - (void)drawRect:(CGRect)rect {
  29. self.layer.cornerRadius = MIN(self.layer.frame.size.height, self.layer.frame.size.width)/2; // this value vary as per your desire
  30. self.clipsToBounds = YES;
  31. }*/
  32. - (void)layoutSubviews {
  33. [super layoutSubviews];
  34. if ([self isRounded]) {
  35. [self addMaskToBounds:self.frame];
  36. }
  37. }
  38. #pragma mark -
  39. -(void)setLightText:(BOOL)light {
  40. if(light) {
  41. UIColor * color = [UIColor colorWithWhite:.84 alpha:1];
  42. [self.dateButton setTitleColor:color forState:UIControlStateNormal];
  43. self.hasItemsIndicator.image = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8 ? [UIImage imageNamed:@"calendar_littledot-disabled" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] : [UIImage imageNamed:@"calendar_littledot-disabled"]);
  44. } else {
  45. UIColor * color = [UIColor colorWithWhite:.3 alpha:1];
  46. [self.dateButton setTitleColor:color forState:UIControlStateNormal];
  47. self.hasItemsIndicator.image = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8 ? [UIImage imageNamed:@"calendar_littledot" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] : [UIImage imageNamed:@"calendar_littledot"]);
  48. }
  49. [self setCurrentColors];
  50. }
  51. - (IBAction)dateButtonTapped:(id)sender {
  52. [self.delegate dateDayTapped:self];
  53. }
  54. -(void)setIsDayInRange{
  55. [self setBackgroundColor:[self.selectedBackgroundColor colorWithAlphaComponent:0.5]];
  56. [self.dateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  57. }
  58. -(void)setSelected:(BOOL)selected {
  59. if(selected) {
  60. [self setBackgroundColor:self.selectedBackgroundColor];
  61. [self.dateButton setSelected:YES];
  62. [self.dateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  63. }
  64. else {
  65. [self setBackgroundColor:[UIColor whiteColor]];
  66. [self.dateButton setSelected:NO];
  67. [self.dateButton setTitleColor:[UIColor colorWithWhite:.3 alpha:1] forState:UIControlStateNormal];
  68. }
  69. [self setCurrentColors];
  70. }
  71. - (void)setCurrentColors {
  72. if (self.currentDateColor && [self isToday]) {
  73. [self.dateButton setTitleColor:self.currentDateColor forState:UIControlStateNormal];
  74. }
  75. if (self.currentDateColorSelected && [self isToday]) {
  76. [self.dateButton setTitleColor:self.currentDateColorSelected forState:UIControlStateSelected];
  77. }
  78. }
  79. -(void)setEnabled:(BOOL)enabled {
  80. [self.dateButton setEnabled:enabled];
  81. if (!enabled) {
  82. [self setLightText:!enabled];
  83. }
  84. }
  85. -(void)indicateDayHasItems:(BOOL)indicate {
  86. self.hasItemsIndicator.hidden = !indicate;
  87. }
  88. - (BOOL)isToday {
  89. static NSCalendar* calendar;
  90. if (!calendar) {
  91. calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  92. }
  93. NSDateComponents *otherDay = [calendar components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:self.date];
  94. NSDateComponents *today = [calendar components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
  95. return ([today day] == [otherDay day] &&
  96. [today month] == [otherDay month] &&
  97. [today year] == [otherDay year] &&
  98. [today era] == [otherDay era]);
  99. }
  100. #pragma mark - Circular mask
  101. - (void)addMaskToBounds:(CGRect)maskBounds {
  102. int minWidthHeight = MIN(maskBounds.size.width, maskBounds.size.height);
  103. CGRect newFrame = CGRectMake(maskBounds.origin.x + ceil((maskBounds.size.width - minWidthHeight)/2.0), maskBounds.origin.y + ceil((maskBounds.size.height - minWidthHeight)/2.0), minWidthHeight, minWidthHeight);
  104. // NSLog(@"x: %f, y: %f, width: %f, height: %f", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height);
  105. CGPathRef maskPath = CGPathCreateWithEllipseInRect(newFrame, NULL);
  106. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  107. maskLayer.bounds = newFrame;
  108. maskLayer.path = maskPath;
  109. maskLayer.fillColor = [UIColor blackColor].CGColor;
  110. CGPathRelease(maskPath);
  111. CGPoint point = CGPointMake(maskBounds.size.width/2, maskBounds.size.height/2);
  112. maskLayer.position = point;
  113. [self.layer setMask:maskLayer];
  114. }
  115. @end