FSCalendarCell.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // FSCalendarCell.m
  3. // Pods
  4. //
  5. // Created by Wenchao Ding on 12/3/15.
  6. //
  7. //
  8. #import "FSCalendarCell.h"
  9. #import "FSCalendar.h"
  10. #import "FSCalendarExtensions.h"
  11. #import "FSCalendarDynamicHeader.h"
  12. #import "FSCalendarConstants.h"
  13. @interface FSCalendarCell ()
  14. @property (readonly, nonatomic) UIColor *colorForCellFill;
  15. @property (readonly, nonatomic) UIColor *colorForTitleLabel;
  16. @property (readonly, nonatomic) UIColor *colorForSubtitleLabel;
  17. @property (readonly, nonatomic) UIColor *colorForCellBorder;
  18. @property (readonly, nonatomic) NSArray<UIColor *> *colorsForEvents;
  19. @property (readonly, nonatomic) CGFloat borderRadius;
  20. @end
  21. @implementation FSCalendarCell
  22. #pragma mark - Life cycle
  23. - (instancetype)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. [self commonInit];
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  32. {
  33. self = [super initWithCoder:aDecoder];
  34. if (self) {
  35. [self commonInit];
  36. }
  37. return self;
  38. }
  39. - (void)commonInit
  40. {
  41. UILabel *label;
  42. CAShapeLayer *shapeLayer;
  43. UIImageView *imageView;
  44. FSCalendarEventIndicator *eventIndicator;
  45. label = [[UILabel alloc] initWithFrame:CGRectZero];
  46. label.textAlignment = NSTextAlignmentCenter;
  47. label.textColor = [UIColor blackColor];
  48. [self.contentView addSubview:label];
  49. self.titleLabel = label;
  50. label = [[UILabel alloc] initWithFrame:CGRectZero];
  51. label.textAlignment = NSTextAlignmentCenter;
  52. label.textColor = [UIColor lightGrayColor];
  53. [self.contentView addSubview:label];
  54. self.subtitleLabel = label;
  55. shapeLayer = [CAShapeLayer layer];
  56. shapeLayer.backgroundColor = [UIColor clearColor].CGColor;
  57. shapeLayer.borderWidth = 1.0;
  58. shapeLayer.borderColor = [UIColor clearColor].CGColor;
  59. shapeLayer.opacity = 0;
  60. [self.contentView.layer insertSublayer:shapeLayer below:_titleLabel.layer];
  61. self.shapeLayer = shapeLayer;
  62. eventIndicator = [[FSCalendarEventIndicator alloc] initWithFrame:CGRectZero];
  63. eventIndicator.backgroundColor = [UIColor clearColor];
  64. eventIndicator.hidden = YES;
  65. [self.contentView addSubview:eventIndicator];
  66. self.eventIndicator = eventIndicator;
  67. imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
  68. imageView.contentMode = UIViewContentModeBottom|UIViewContentModeCenter;
  69. [self.contentView addSubview:imageView];
  70. self.imageView = imageView;
  71. self.clipsToBounds = NO;
  72. self.contentView.clipsToBounds = NO;
  73. }
  74. - (void)layoutSubviews
  75. {
  76. [super layoutSubviews];
  77. if (_subtitle) {
  78. _subtitleLabel.text = _subtitle;
  79. if (_subtitleLabel.hidden) {
  80. _subtitleLabel.hidden = NO;
  81. }
  82. } else {
  83. if (!_subtitleLabel.hidden) {
  84. _subtitleLabel.hidden = YES;
  85. }
  86. }
  87. if (_subtitle) {
  88. CGFloat titleHeight = self.titleLabel.font.lineHeight;
  89. CGFloat subtitleHeight = self.subtitleLabel.font.lineHeight;
  90. CGFloat height = titleHeight + subtitleHeight;
  91. _titleLabel.frame = CGRectMake(
  92. self.preferredTitleOffset.x,
  93. (self.contentView.fs_height*5.0/6.0-height)*0.5+self.preferredTitleOffset.y,
  94. self.contentView.fs_width,
  95. titleHeight
  96. );
  97. _subtitleLabel.frame = CGRectMake(
  98. self.preferredSubtitleOffset.x,
  99. (_titleLabel.fs_bottom-self.preferredTitleOffset.y) - (_titleLabel.fs_height-_titleLabel.font.pointSize)+self.preferredSubtitleOffset.y,
  100. self.contentView.fs_width,
  101. subtitleHeight
  102. );
  103. } else {
  104. _titleLabel.frame = CGRectMake(
  105. self.preferredTitleOffset.x,
  106. self.preferredTitleOffset.y,
  107. self.contentView.fs_width,
  108. floor(self.contentView.fs_height*5.0/6.0)
  109. );
  110. }
  111. _imageView.frame = CGRectMake(self.preferredImageOffset.x, self.preferredImageOffset.y, self.contentView.fs_width, self.contentView.fs_height);
  112. CGFloat titleHeight = self.bounds.size.height*5.0/6.0;
  113. CGFloat diameter = MIN(self.bounds.size.height*5.0/6.0,self.bounds.size.width);
  114. diameter = diameter > FSCalendarStandardCellDiameter ? (diameter - (diameter-FSCalendarStandardCellDiameter)*0.5) : diameter;
  115. _shapeLayer.frame = CGRectMake((self.bounds.size.width-diameter)/2,
  116. (titleHeight-diameter)/2,
  117. diameter,
  118. diameter);
  119. CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:_shapeLayer.bounds
  120. cornerRadius:CGRectGetWidth(_shapeLayer.bounds)*0.5*self.borderRadius].CGPath;
  121. if (!CGPathEqualToPath(_shapeLayer.path,path)) {
  122. _shapeLayer.path = path;
  123. }
  124. CGFloat eventSize = _shapeLayer.frame.size.height/6.0;
  125. _eventIndicator.frame = CGRectMake(
  126. self.preferredEventOffset.x,
  127. CGRectGetMaxY(_shapeLayer.frame)+eventSize*0.17+self.preferredEventOffset.y,
  128. self.fs_width,
  129. eventSize*0.83
  130. );
  131. }
  132. - (void)prepareForReuse
  133. {
  134. [super prepareForReuse];
  135. if (self.window) { // Avoid interrupt of navigation transition somehow
  136. [CATransaction setDisableActions:YES]; // Avoid blink of shape layer.
  137. }
  138. self.shapeLayer.opacity = 0;
  139. [self.contentView.layer removeAnimationForKey:@"opacity"];
  140. }
  141. #pragma mark - Public
  142. - (void)performSelecting
  143. {
  144. _shapeLayer.opacity = 1;
  145. CAAnimationGroup *group = [CAAnimationGroup animation];
  146. CABasicAnimation *zoomOut = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  147. zoomOut.fromValue = @0.3;
  148. zoomOut.toValue = @1.2;
  149. zoomOut.duration = FSCalendarDefaultBounceAnimationDuration/4*3;
  150. CABasicAnimation *zoomIn = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  151. zoomIn.fromValue = @1.2;
  152. zoomIn.toValue = @1.0;
  153. zoomIn.beginTime = FSCalendarDefaultBounceAnimationDuration/4*3;
  154. zoomIn.duration = FSCalendarDefaultBounceAnimationDuration/4;
  155. group.duration = FSCalendarDefaultBounceAnimationDuration;
  156. group.animations = @[zoomOut, zoomIn];
  157. [_shapeLayer addAnimation:group forKey:@"bounce"];
  158. [self configureAppearance];
  159. }
  160. #pragma mark - Private
  161. - (void)configureAppearance
  162. {
  163. UIColor *textColor = self.colorForTitleLabel;
  164. if (![textColor isEqual:_titleLabel.textColor]) {
  165. _titleLabel.textColor = textColor;
  166. }
  167. UIFont *titleFont = self.calendar.appearance.titleFont;
  168. if (![titleFont isEqual:_titleLabel.font]) {
  169. _titleLabel.font = titleFont;
  170. }
  171. if (_subtitle) {
  172. textColor = self.colorForSubtitleLabel;
  173. if (![textColor isEqual:_subtitleLabel.textColor]) {
  174. _subtitleLabel.textColor = textColor;
  175. }
  176. titleFont = self.calendar.appearance.subtitleFont;
  177. if (![titleFont isEqual:_subtitleLabel.font]) {
  178. _subtitleLabel.font = titleFont;
  179. }
  180. }
  181. UIColor *borderColor = self.colorForCellBorder;
  182. UIColor *fillColor = self.colorForCellFill;
  183. BOOL shouldHideShapeLayer = !self.selected && !self.dateIsToday && !borderColor && !fillColor;
  184. if (_shapeLayer.opacity == shouldHideShapeLayer) {
  185. _shapeLayer.opacity = !shouldHideShapeLayer;
  186. }
  187. if (!shouldHideShapeLayer) {
  188. CGColorRef cellFillColor = self.colorForCellFill.CGColor;
  189. if (!CGColorEqualToColor(_shapeLayer.fillColor, cellFillColor)) {
  190. _shapeLayer.fillColor = cellFillColor;
  191. }
  192. CGColorRef cellBorderColor = self.colorForCellBorder.CGColor;
  193. if (!CGColorEqualToColor(_shapeLayer.strokeColor, cellBorderColor)) {
  194. _shapeLayer.strokeColor = cellBorderColor;
  195. }
  196. CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:_shapeLayer.bounds
  197. cornerRadius:CGRectGetWidth(_shapeLayer.bounds)*0.5*self.borderRadius].CGPath;
  198. if (!CGPathEqualToPath(_shapeLayer.path, path)) {
  199. _shapeLayer.path = path;
  200. }
  201. }
  202. if (![_image isEqual:_imageView.image]) {
  203. _imageView.image = _image;
  204. _imageView.hidden = !_image;
  205. }
  206. if (_eventIndicator.hidden == (_numberOfEvents > 0)) {
  207. _eventIndicator.hidden = !_numberOfEvents;
  208. }
  209. _eventIndicator.numberOfEvents = self.numberOfEvents;
  210. _eventIndicator.color = self.colorsForEvents;
  211. }
  212. - (UIColor *)colorForCurrentStateInDictionary:(NSDictionary *)dictionary
  213. {
  214. if (self.isSelected) {
  215. if (self.dateIsToday) {
  216. return dictionary[@(FSCalendarCellStateSelected|FSCalendarCellStateToday)] ?: dictionary[@(FSCalendarCellStateSelected)];
  217. }
  218. return dictionary[@(FSCalendarCellStateSelected)];
  219. }
  220. if (self.dateIsToday && [[dictionary allKeys] containsObject:@(FSCalendarCellStateToday)]) {
  221. return dictionary[@(FSCalendarCellStateToday)];
  222. }
  223. if (self.placeholder && [[dictionary allKeys] containsObject:@(FSCalendarCellStatePlaceholder)]) {
  224. return dictionary[@(FSCalendarCellStatePlaceholder)];
  225. }
  226. if (self.weekend && [[dictionary allKeys] containsObject:@(FSCalendarCellStateWeekend)]) {
  227. return dictionary[@(FSCalendarCellStateWeekend)];
  228. }
  229. return dictionary[@(FSCalendarCellStateNormal)];
  230. }
  231. #pragma mark - Properties
  232. - (UIColor *)colorForCellFill
  233. {
  234. if (self.selected) {
  235. return self.preferredFillSelectionColor ?: [self colorForCurrentStateInDictionary:_appearance.backgroundColors];
  236. }
  237. return self.preferredFillDefaultColor ?: [self colorForCurrentStateInDictionary:_appearance.backgroundColors];
  238. }
  239. - (UIColor *)colorForTitleLabel
  240. {
  241. if (self.selected) {
  242. return self.preferredTitleSelectionColor ?: [self colorForCurrentStateInDictionary:_appearance.titleColors];
  243. }
  244. return self.preferredTitleDefaultColor ?: [self colorForCurrentStateInDictionary:_appearance.titleColors];
  245. }
  246. - (UIColor *)colorForSubtitleLabel
  247. {
  248. if (self.selected) {
  249. return self.preferredSubtitleSelectionColor ?: [self colorForCurrentStateInDictionary:_appearance.subtitleColors];
  250. }
  251. return self.preferredSubtitleDefaultColor ?: [self colorForCurrentStateInDictionary:_appearance.subtitleColors];
  252. }
  253. - (UIColor *)colorForCellBorder
  254. {
  255. if (self.selected) {
  256. return _preferredBorderSelectionColor ?: _appearance.borderSelectionColor;
  257. }
  258. return _preferredBorderDefaultColor ?: _appearance.borderDefaultColor;
  259. }
  260. - (NSArray<UIColor *> *)colorsForEvents
  261. {
  262. if (self.selected) {
  263. return _preferredEventSelectionColors ?: @[_appearance.eventSelectionColor];
  264. }
  265. return _preferredEventDefaultColors ?: @[_appearance.eventDefaultColor];
  266. }
  267. - (CGFloat)borderRadius
  268. {
  269. return _preferredBorderRadius >= 0 ? _preferredBorderRadius : _appearance.borderRadius;
  270. }
  271. #define OFFSET_PROPERTY(NAME,CAPITAL,ALTERNATIVE) \
  272. \
  273. @synthesize NAME = _##NAME; \
  274. \
  275. - (void)set##CAPITAL:(CGPoint)NAME \
  276. { \
  277. BOOL diff = !CGPointEqualToPoint(NAME, self.NAME); \
  278. _##NAME = NAME; \
  279. if (diff) { \
  280. [self setNeedsLayout]; \
  281. } \
  282. } \
  283. \
  284. - (CGPoint)NAME \
  285. { \
  286. return CGPointEqualToPoint(_##NAME, CGPointInfinity) ? ALTERNATIVE : _##NAME; \
  287. }
  288. OFFSET_PROPERTY(preferredTitleOffset, PreferredTitleOffset, _appearance.titleOffset);
  289. OFFSET_PROPERTY(preferredSubtitleOffset, PreferredSubtitleOffset, _appearance.subtitleOffset);
  290. OFFSET_PROPERTY(preferredImageOffset, PreferredImageOffset, _appearance.imageOffset);
  291. OFFSET_PROPERTY(preferredEventOffset, PreferredEventOffset, _appearance.eventOffset);
  292. #undef OFFSET_PROPERTY
  293. - (void)setCalendar:(FSCalendar *)calendar
  294. {
  295. if (![_calendar isEqual:calendar]) {
  296. _calendar = calendar;
  297. _appearance = calendar.appearance;
  298. [self configureAppearance];
  299. }
  300. }
  301. - (void)setSubtitle:(NSString *)subtitle
  302. {
  303. if (![_subtitle isEqualToString:subtitle]) {
  304. BOOL diff = (subtitle.length && !_subtitle.length) || (_subtitle.length && !subtitle.length);
  305. _subtitle = subtitle;
  306. if (diff) {
  307. [self setNeedsLayout];
  308. }
  309. }
  310. }
  311. @end
  312. @interface FSCalendarEventIndicator ()
  313. @property (weak, nonatomic) UIView *contentView;
  314. @property (strong, nonatomic) NSPointerArray *eventLayers;
  315. @end
  316. @implementation FSCalendarEventIndicator
  317. - (instancetype)initWithFrame:(CGRect)frame
  318. {
  319. self = [super initWithFrame:frame];
  320. if (self) {
  321. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  322. [self addSubview:view];
  323. self.contentView = view;
  324. self.eventLayers = [NSPointerArray weakObjectsPointerArray];
  325. for (int i = 0; i < FSCalendarMaximumNumberOfEvents; i++) {
  326. CALayer *layer = [CALayer layer];
  327. layer.backgroundColor = [UIColor clearColor].CGColor;
  328. [self.contentView.layer addSublayer:layer];
  329. [self.eventLayers addPointer:(__bridge void * _Nullable)(layer)];
  330. }
  331. }
  332. return self;
  333. }
  334. - (void)layoutSubviews
  335. {
  336. [super layoutSubviews];
  337. CGFloat diameter = MIN(MIN(self.fs_width, self.fs_height),FSCalendarMaximumEventDotDiameter);
  338. self.contentView.fs_height = self.fs_height;
  339. self.contentView.fs_width = (self.numberOfEvents*2-1)*diameter;
  340. self.contentView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  341. }
  342. - (void)layoutSublayersOfLayer:(CALayer *)layer
  343. {
  344. [super layoutSublayersOfLayer:layer];
  345. if (layer == self.layer) {
  346. CGFloat diameter = MIN(MIN(self.fs_width, self.fs_height),FSCalendarMaximumEventDotDiameter);
  347. for (int i = 0; i < self.eventLayers.count; i++) {
  348. CALayer *eventLayer = [self.eventLayers pointerAtIndex:i];
  349. eventLayer.hidden = i >= self.numberOfEvents;
  350. if (!eventLayer.hidden) {
  351. eventLayer.frame = CGRectMake(2*i*diameter, (self.fs_height-diameter)*0.5, diameter, diameter);
  352. if (eventLayer.cornerRadius != diameter/2) {
  353. eventLayer.cornerRadius = diameter/2;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. - (void)setColor:(id)color
  360. {
  361. if (![_color isEqual:color]) {
  362. _color = color;
  363. if ([_color isKindOfClass:[UIColor class]]) {
  364. for (NSInteger i = 0; i < self.eventLayers.count; i++) {
  365. CALayer *layer = [self.eventLayers pointerAtIndex:i];
  366. layer.backgroundColor = [_color CGColor];
  367. }
  368. } else if ([_color isKindOfClass:[NSArray class]]) {
  369. NSArray<UIColor *> *colors = (NSArray *)_color;
  370. for (int i = 0; i < self.eventLayers.count; i++) {
  371. CALayer *eventLayer = [self.eventLayers pointerAtIndex:i];
  372. eventLayer.backgroundColor = colors[MIN(i,colors.count-1)].CGColor;
  373. }
  374. }
  375. }
  376. }
  377. - (void)setNumberOfEvents:(NSInteger)numberOfEvents
  378. {
  379. if (_numberOfEvents != numberOfEvents) {
  380. _numberOfEvents = MIN(MAX(numberOfEvents,0),FSCalendarMaximumNumberOfEvents);
  381. [self setNeedsLayout];
  382. }
  383. }
  384. @end
  385. @implementation FSCalendarBlankCell
  386. - (void)configureAppearance {}
  387. @end