MJRefreshStateHeader.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // MJRefreshStateHeader.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshStateHeader.h"
  9. @interface MJRefreshStateHeader()
  10. {
  11. /** 显示上一次刷新时间的label */
  12. __unsafe_unretained UILabel *_lastUpdatedTimeLabel;
  13. /** 显示刷新状态的label */
  14. __unsafe_unretained UILabel *_stateLabel;
  15. }
  16. /** 所有状态对应的文字 */
  17. @property (strong, nonatomic) NSMutableDictionary *stateTitles;
  18. @end
  19. @implementation MJRefreshStateHeader
  20. #pragma mark - 懒加载
  21. - (NSMutableDictionary *)stateTitles
  22. {
  23. if (!_stateTitles) {
  24. self.stateTitles = [NSMutableDictionary dictionary];
  25. }
  26. return _stateTitles;
  27. }
  28. - (UILabel *)stateLabel
  29. {
  30. if (!_stateLabel) {
  31. [self addSubview:_stateLabel = [UILabel mj_label]];
  32. }
  33. return _stateLabel;
  34. }
  35. - (UILabel *)lastUpdatedTimeLabel
  36. {
  37. if (!_lastUpdatedTimeLabel) {
  38. [self addSubview:_lastUpdatedTimeLabel = [UILabel mj_label]];
  39. }
  40. return _lastUpdatedTimeLabel;
  41. }
  42. #pragma mark - 公共方法
  43. - (void)setTitle:(NSString *)title forState:(MJRefreshState)state
  44. {
  45. if (title == nil) return;
  46. self.stateTitles[@(state)] = title;
  47. self.stateLabel.text = self.stateTitles[@(self.state)];
  48. }
  49. #pragma mark - 日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。
  50. - (NSCalendar *)currentCalendar {
  51. if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
  52. return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  53. }
  54. return [NSCalendar currentCalendar];
  55. }
  56. #pragma mark key的处理
  57. - (void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey
  58. {
  59. [super setLastUpdatedTimeKey:lastUpdatedTimeKey];
  60. // 如果label隐藏了,就不用再处理
  61. if (self.lastUpdatedTimeLabel.hidden) return;
  62. NSDate *lastUpdatedTime = [[NSUserDefaults standardUserDefaults] objectForKey:lastUpdatedTimeKey];
  63. // 如果有block
  64. if (self.lastUpdatedTimeText) {
  65. self.lastUpdatedTimeLabel.text = self.lastUpdatedTimeText(lastUpdatedTime);
  66. return;
  67. }
  68. if (lastUpdatedTime) {
  69. // 1.获得年月日
  70. NSCalendar *calendar = [self currentCalendar];
  71. NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
  72. NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
  73. NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
  74. // 2.格式化日期
  75. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  76. BOOL isToday = NO;
  77. if ([cmp1 day] == [cmp2 day]) { // 今天
  78. formatter.dateFormat = @" HH:mm";
  79. isToday = YES;
  80. } else if ([cmp1 year] == [cmp2 year]) { // 今年
  81. formatter.dateFormat = @"MM-dd HH:mm";
  82. } else {
  83. formatter.dateFormat = @"yyyy-MM-dd HH:mm";
  84. }
  85. NSString *time = [formatter stringFromDate:lastUpdatedTime];
  86. // 3.显示日期
  87. self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@%@",
  88. [NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
  89. isToday ? [NSBundle mj_localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
  90. time];
  91. } else {
  92. self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@",
  93. [NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
  94. [NSBundle mj_localizedStringForKey:MJRefreshHeaderNoneLastDateText]];
  95. }
  96. }
  97. #pragma mark - 覆盖父类的方法
  98. - (void)prepare
  99. {
  100. [super prepare];
  101. // 初始化间距
  102. self.labelLeftInset = MJRefreshLabelLeftInset;
  103. // 初始化文字
  104. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderIdleText] forState:MJRefreshStateIdle];
  105. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderPullingText] forState:MJRefreshStatePulling];
  106. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshHeaderRefreshingText] forState:MJRefreshStateRefreshing];
  107. }
  108. - (void)placeSubviews
  109. {
  110. [super placeSubviews];
  111. if (self.stateLabel.hidden) return;
  112. BOOL noConstrainsOnStatusLabel = self.stateLabel.constraints.count == 0;
  113. if (self.lastUpdatedTimeLabel.hidden) {
  114. // 状态
  115. if (noConstrainsOnStatusLabel) self.stateLabel.frame = self.bounds;
  116. } else {
  117. CGFloat stateLabelH = self.mj_h * 0.5;
  118. // 状态
  119. if (noConstrainsOnStatusLabel) {
  120. self.stateLabel.mj_x = 0;
  121. self.stateLabel.mj_y = 0;
  122. self.stateLabel.mj_w = self.mj_w;
  123. self.stateLabel.mj_h = stateLabelH;
  124. }
  125. // 更新时间
  126. if (self.lastUpdatedTimeLabel.constraints.count == 0) {
  127. self.lastUpdatedTimeLabel.mj_x = 0;
  128. self.lastUpdatedTimeLabel.mj_y = stateLabelH;
  129. self.lastUpdatedTimeLabel.mj_w = self.mj_w;
  130. self.lastUpdatedTimeLabel.mj_h = self.mj_h - self.lastUpdatedTimeLabel.mj_y;
  131. }
  132. }
  133. }
  134. - (void)setState:(MJRefreshState)state
  135. {
  136. MJRefreshCheckState
  137. // 设置状态文字
  138. self.stateLabel.text = self.stateTitles[@(state)];
  139. // 重新设置key(重新显示时间)
  140. self.lastUpdatedTimeKey = self.lastUpdatedTimeKey;
  141. }
  142. @end