GHBlankView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // GHBlankView.m
  3. // GameHelper
  4. //
  5. // Created by 青秀斌 on 16/12/29.
  6. // Copyright © 2016年 kylincc. All rights reserved.
  7. //
  8. #import "GHBlankView.h"
  9. #import "UILabel+GHConfig.h"
  10. #import "UIView+GHTheme.h"
  11. #import <Masonry.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. @interface GHBlankView ()
  14. @property (strong, nonatomic) UIView *contentView;
  15. @property (nullable, strong, nonatomic) UIImageView *imageView;
  16. @property (nullable, strong, nonatomic) UILabel *titleLabel;
  17. @property (nullable, strong, nonatomic) UILabel *messageLabel;
  18. @property (assign, nonatomic) CGFloat topOffset;
  19. @property (strong, nonatomic) NSString *imageName;
  20. @property (copy, nonatomic) BlankImageBlock image;
  21. @property (copy, nonatomic) BlankTextBlock title;
  22. @property (copy, nonatomic) BlankTextBlock message;
  23. @property (copy, nonatomic) BlankOffsetBlock offsetY;
  24. @end
  25. @implementation GHBlankView
  26. - (void)dealloc {
  27. [self unObserveThemeChange];
  28. }
  29. - (instancetype)init {
  30. if (self = [super init]) {
  31. [self observeThemeChange];
  32. }
  33. return self;
  34. }
  35. /**********************************************************************/
  36. #pragma mark - OverWrite
  37. /**********************************************************************/
  38. - (void)updateConstraints {
  39. [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  40. make.centerX.equalTo(self);
  41. make.centerY.equalTo(self).offset(self.topOffset);
  42. make.left.equalTo(self).offset(20);
  43. }];
  44. UIView *tempView = nil;
  45. if (self.imageView) {
  46. [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  47. make.top.equalTo(self.contentView);
  48. make.centerX.equalTo(self.contentView);
  49. make.right.lessThanOrEqualTo(self.contentView);
  50. }];
  51. tempView = self.imageView;
  52. }
  53. if (self.titleLabel) {
  54. [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  55. if (tempView && tempView == self.imageView) {
  56. make.top.equalTo(tempView.mas_bottom).offset(20);
  57. } else {
  58. make.top.equalTo(self.contentView);
  59. }
  60. make.centerX.equalTo(self.contentView);
  61. make.right.lessThanOrEqualTo(self.contentView);
  62. }];
  63. tempView = self.titleLabel;
  64. }
  65. if (self.messageLabel) {
  66. [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  67. if (tempView && tempView == self.titleLabel) {
  68. make.top.equalTo(tempView.mas_bottom).offset(8);
  69. } else if (tempView && tempView == self.imageView) {
  70. make.top.equalTo(tempView.mas_bottom).offset(20);
  71. } else {
  72. make.top.equalTo(self.contentView);
  73. }
  74. make.centerX.equalTo(self.contentView);
  75. make.right.lessThanOrEqualTo(self.contentView);
  76. }];
  77. tempView = self.messageLabel;
  78. }
  79. if (tempView) {
  80. [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
  81. make.bottom.equalTo(self.contentView);
  82. }];
  83. }
  84. [super updateConstraints];
  85. }
  86. - (void)themeChangeAction {
  87. self.imageView.image = self.imageName?[UIImage imageNamed:self.imageName]:nil;
  88. self.titleLabel.configText = @"T2";
  89. self.messageLabel.configText = @"T2";
  90. }
  91. /**********************************************************************/
  92. #pragma mark - Private
  93. /**********************************************************************/
  94. - (UIView *)contentView {
  95. if (!_contentView) {
  96. _contentView = [[UIView alloc] init];
  97. [self addSubview:_contentView];
  98. }
  99. return _contentView;
  100. }
  101. /**********************************************************************/
  102. #pragma mark - Public
  103. /**********************************************************************/
  104. - (BlankImageBlock)image {
  105. return ^GHBlankView *(NSString *image) {
  106. self.imageName = image;
  107. if (image) {
  108. if (!self.imageView) {
  109. self.imageView = [[UIImageView alloc] init];
  110. self.imageView.backgroundColor = [UIColor clearColor];
  111. self.imageView.contentMode = UIViewContentModeCenter;
  112. [self.contentView addSubview:self.imageView];
  113. }
  114. self.imageView.image = [UIImage imageNamed:image];
  115. } else {
  116. if (self.imageView) {
  117. [self.imageView removeFromSuperview];
  118. self.imageView = nil;
  119. }
  120. }
  121. return self;
  122. };
  123. }
  124. - (BlankTextBlock)title {
  125. return ^GHBlankView *(NSString *title) {
  126. if (title) {
  127. if (!self.titleLabel) {
  128. self.titleLabel = [[UILabel alloc] init];
  129. // self.titleLabel.configText = @"T2";
  130. [self.titleLabel setTextColor:RGB(42, 42, 42)];
  131. self.titleLabel.backgroundColor = [UIColor clearColor];
  132. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  133. self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
  134. self.titleLabel.numberOfLines = 2;
  135. [self.contentView addSubview:self.titleLabel];
  136. }
  137. self.titleLabel.text = title;
  138. } else {
  139. if (self.titleLabel) {
  140. [self.titleLabel removeFromSuperview];
  141. self.titleLabel = nil;
  142. }
  143. }
  144. return self;
  145. };
  146. }
  147. - (BlankTextBlock)message {
  148. return ^GHBlankView *(NSString *message) {
  149. if (message) {
  150. if (!self.messageLabel) {
  151. self.messageLabel = [[UILabel alloc] init];
  152. self.messageLabel.configText = @"T2";
  153. self.messageLabel.backgroundColor = [UIColor clearColor];
  154. self.messageLabel.textAlignment = NSTextAlignmentCenter;
  155. self.messageLabel.font = [UIFont systemFontOfSize:16];
  156. self.messageLabel.numberOfLines = 2;
  157. [self.contentView addSubview:self.messageLabel];
  158. }
  159. self.messageLabel.text = message;
  160. } else {
  161. if (self.messageLabel) {
  162. [self.messageLabel removeFromSuperview];
  163. self.messageLabel = nil;
  164. }
  165. }
  166. return self;
  167. };
  168. }
  169. - (BlankOffsetBlock)offsetY {
  170. return ^GHBlankView *(CGFloat offsetY) {
  171. self.topOffset = offsetY;
  172. [self setNeedsUpdateConstraints];
  173. [self setNeedsLayout];
  174. return self;
  175. };
  176. }
  177. @end
  178. NS_ASSUME_NONNULL_END