FSCalendarStickyHeader.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // FSCalendarStaticHeader.m
  3. // FSCalendar
  4. //
  5. // Created by dingwenchao on 9/17/15.
  6. // Copyright (c) 2015 Wenchao Ding. All rights reserved.
  7. //
  8. #import "FSCalendarStickyHeader.h"
  9. #import "FSCalendar.h"
  10. #import "FSCalendarWeekdayView.h"
  11. #import "FSCalendarExtensions.h"
  12. #import "FSCalendarConstants.h"
  13. #import "FSCalendarDynamicHeader.h"
  14. @interface FSCalendarStickyHeader ()
  15. @property (weak , nonatomic) UIView *contentView;
  16. @property (weak , nonatomic) UIView *bottomBorder;
  17. @property (weak , nonatomic) FSCalendarWeekdayView *weekdayView;
  18. @end
  19. @implementation FSCalendarStickyHeader
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. UIView *view;
  25. UILabel *label;
  26. view = [[UIView alloc] initWithFrame:CGRectZero];
  27. view.backgroundColor = [UIColor clearColor];
  28. [self addSubview:view];
  29. self.contentView = view;
  30. label = [[UILabel alloc] initWithFrame:CGRectZero];
  31. label.textAlignment = NSTextAlignmentCenter;
  32. label.numberOfLines = 0;
  33. [_contentView addSubview:label];
  34. self.titleLabel = label;
  35. view = [[UIView alloc] initWithFrame:CGRectZero];
  36. view.backgroundColor = FSCalendarStandardLineColor;
  37. [_contentView addSubview:view];
  38. self.bottomBorder = view;
  39. FSCalendarWeekdayView *weekdayView = [[FSCalendarWeekdayView alloc] init];
  40. [self.contentView addSubview:weekdayView];
  41. self.weekdayView = weekdayView;
  42. }
  43. return self;
  44. }
  45. - (void)layoutSubviews
  46. {
  47. [super layoutSubviews];
  48. _contentView.frame = self.bounds;
  49. CGFloat weekdayHeight = _calendar.preferredWeekdayHeight;
  50. CGFloat weekdayMargin = weekdayHeight * 0.1;
  51. CGFloat titleWidth = _contentView.fs_width;
  52. self.weekdayView.frame = CGRectMake(0, _contentView.fs_height-weekdayHeight-weekdayMargin, self.contentView.fs_width, weekdayHeight);
  53. CGFloat titleHeight = [@"1" sizeWithAttributes:@{NSFontAttributeName:self.calendar.appearance.headerTitleFont}].height*1.5 + weekdayMargin*3;
  54. _bottomBorder.frame = CGRectMake(0, _contentView.fs_height-weekdayHeight-weekdayMargin*2, _contentView.fs_width, 1.0);
  55. CGPoint titleHeaderOffset = self.calendar.appearance.headerTitleOffset;
  56. _titleLabel.frame = CGRectMake(titleHeaderOffset.x,
  57. titleHeaderOffset.y+_bottomBorder.fs_bottom-titleHeight-weekdayMargin,
  58. titleWidth,
  59. titleHeight);
  60. }
  61. #pragma mark - Properties
  62. - (void)setCalendar:(FSCalendar *)calendar
  63. {
  64. if (![_calendar isEqual:calendar]) {
  65. _calendar = calendar;
  66. _weekdayView.calendar = calendar;
  67. [self configureAppearance];
  68. }
  69. }
  70. #pragma mark - Private methods
  71. - (void)configureAppearance
  72. {
  73. _titleLabel.font = self.calendar.appearance.headerTitleFont;
  74. _titleLabel.textColor = self.calendar.appearance.headerTitleColor;
  75. _titleLabel.textAlignment = self.calendar.appearance.headerTitleAlignment;
  76. _bottomBorder.backgroundColor = self.calendar.appearance.headerSeparatorColor;
  77. [self.weekdayView configureAppearance];
  78. }
  79. - (void)setMonth:(NSDate *)month
  80. {
  81. _month = month;
  82. _calendar.formatter.dateFormat = self.calendar.appearance.headerDateFormat;
  83. BOOL usesUpperCase = (self.calendar.appearance.caseOptions & 15) == FSCalendarCaseOptionsHeaderUsesUpperCase;
  84. BOOL usesCapitalized = (self.calendar.appearance.caseOptions & 15) == FSCalendarCaseOptionsHeaderUsesCapitalized;
  85. NSString *text = [_calendar.formatter stringFromDate:_month];
  86. if (usesUpperCase) {
  87. text = text.uppercaseString;
  88. } else if (usesCapitalized) {
  89. text = text.capitalizedString;
  90. }
  91. self.titleLabel.text = text;
  92. }
  93. @end