123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- //
- // GHBlankView.m
- // GameHelper
- //
- // Created by 青秀斌 on 16/12/29.
- // Copyright © 2016年 kylincc. All rights reserved.
- //
- #import "GHBlankView.h"
- #import "UILabel+GHConfig.h"
- #import "UIView+GHTheme.h"
- #import <Masonry.h>
- NS_ASSUME_NONNULL_BEGIN
- @interface GHBlankView ()
- @property (strong, nonatomic) UIView *contentView;
- @property (nullable, strong, nonatomic) UIImageView *imageView;
- @property (nullable, strong, nonatomic) UILabel *titleLabel;
- @property (nullable, strong, nonatomic) UILabel *messageLabel;
- @property (assign, nonatomic) CGFloat topOffset;
- @property (strong, nonatomic) NSString *imageName;
- @property (copy, nonatomic) BlankImageBlock image;
- @property (copy, nonatomic) BlankTextBlock title;
- @property (copy, nonatomic) BlankTextBlock message;
- @property (copy, nonatomic) BlankOffsetBlock offsetY;
- @end
- @implementation GHBlankView
- - (void)dealloc {
- [self unObserveThemeChange];
- }
- - (instancetype)init {
- if (self = [super init]) {
- [self observeThemeChange];
- }
- return self;
- }
- /**********************************************************************/
- #pragma mark - OverWrite
- /**********************************************************************/
- - (void)updateConstraints {
-
- [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self);
- make.centerY.equalTo(self).offset(self.topOffset);
- make.left.equalTo(self).offset(20);
- }];
-
- UIView *tempView = nil;
- if (self.imageView) {
- [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.contentView);
- make.centerX.equalTo(self.contentView);
- make.right.lessThanOrEqualTo(self.contentView);
- }];
- tempView = self.imageView;
- }
-
- if (self.titleLabel) {
- [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- if (tempView && tempView == self.imageView) {
- make.top.equalTo(tempView.mas_bottom).offset(20);
- } else {
- make.top.equalTo(self.contentView);
- }
- make.centerX.equalTo(self.contentView);
- make.right.lessThanOrEqualTo(self.contentView);
- }];
- tempView = self.titleLabel;
- }
-
- if (self.messageLabel) {
- [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
- if (tempView && tempView == self.titleLabel) {
- make.top.equalTo(tempView.mas_bottom).offset(8);
- } else if (tempView && tempView == self.imageView) {
- make.top.equalTo(tempView.mas_bottom).offset(20);
- } else {
- make.top.equalTo(self.contentView);
- }
- make.centerX.equalTo(self.contentView);
- make.right.lessThanOrEqualTo(self.contentView);
- }];
- tempView = self.messageLabel;
- }
-
- if (tempView) {
- [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.contentView);
- }];
- }
-
- [super updateConstraints];
- }
- - (void)themeChangeAction {
- self.imageView.image = self.imageName?[UIImage imageNamed:self.imageName]:nil;
- self.titleLabel.configText = @"T2";
- self.messageLabel.configText = @"T2";
- }
- /**********************************************************************/
- #pragma mark - Private
- /**********************************************************************/
- - (UIView *)contentView {
- if (!_contentView) {
- _contentView = [[UIView alloc] init];
- [self addSubview:_contentView];
- }
- return _contentView;
- }
- /**********************************************************************/
- #pragma mark - Public
- /**********************************************************************/
- - (BlankImageBlock)image {
- return ^GHBlankView *(NSString *image) {
- self.imageName = image;
- if (image) {
- if (!self.imageView) {
- self.imageView = [[UIImageView alloc] init];
- self.imageView.backgroundColor = [UIColor clearColor];
- self.imageView.contentMode = UIViewContentModeCenter;
- [self.contentView addSubview:self.imageView];
- }
- self.imageView.image = [UIImage imageNamed:image];
- } else {
- if (self.imageView) {
- [self.imageView removeFromSuperview];
- self.imageView = nil;
- }
- }
- return self;
- };
- }
- - (BlankTextBlock)title {
- return ^GHBlankView *(NSString *title) {
- if (title) {
- if (!self.titleLabel) {
- self.titleLabel = [[UILabel alloc] init];
- // self.titleLabel.configText = @"T2";
- [self.titleLabel setTextColor:RGB(42, 42, 42)];
- self.titleLabel.backgroundColor = [UIColor clearColor];
- self.titleLabel.textAlignment = NSTextAlignmentCenter;
- self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
- self.titleLabel.numberOfLines = 2;
- [self.contentView addSubview:self.titleLabel];
- }
- self.titleLabel.text = title;
- } else {
- if (self.titleLabel) {
- [self.titleLabel removeFromSuperview];
- self.titleLabel = nil;
- }
- }
- return self;
- };
- }
- - (BlankTextBlock)message {
- return ^GHBlankView *(NSString *message) {
- if (message) {
- if (!self.messageLabel) {
- self.messageLabel = [[UILabel alloc] init];
- self.messageLabel.configText = @"T2";
- self.messageLabel.backgroundColor = [UIColor clearColor];
- self.messageLabel.textAlignment = NSTextAlignmentCenter;
- self.messageLabel.font = [UIFont systemFontOfSize:16];
- self.messageLabel.numberOfLines = 2;
- [self.contentView addSubview:self.messageLabel];
- }
- self.messageLabel.text = message;
- } else {
- if (self.messageLabel) {
- [self.messageLabel removeFromSuperview];
- self.messageLabel = nil;
- }
- }
- return self;
- };
- }
- - (BlankOffsetBlock)offsetY {
- return ^GHBlankView *(CGFloat offsetY) {
- self.topOffset = offsetY;
- [self setNeedsUpdateConstraints];
- [self setNeedsLayout];
- return self;
- };
- }
- @end
- NS_ASSUME_NONNULL_END
|