// // TFEmptyView.m // WXReader // // Created by 谢腾飞 on 2020/11/20. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFEmptyView.h" // 每个子控件之间的间距 #define kSubViewMargin 20.f // 描述字体 #define kTitleLabFont [UIFont systemFontOfSize:16.f] // 详细描述字体 #define kDetailLabFont [UIFont systemFontOfSize:14.f] // 按钮字体大小 #define kActionBtnFont [UIFont systemFontOfSize:14.f] // 按钮高度 #define kActionBtnHeight 40.f // 水平方向内边距 #define kActionBtnHorizontalMargin 30.f // 灰色 #define kGrayColor [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.f] @interface TFEmptyView () @property (nonatomic ,strong) UILabel *titleLabel; @property (nonatomic ,strong) UILabel *detailLabel; @property (nonatomic ,strong) UIButton *actionButton; @end @implementation TFEmptyView { CGFloat _contentMaxWidth; // 最大宽度 CGFloat _contentWidth; // 内容物宽度 CGFloat _contentHeight; // 内容物高度 CGFloat _subViweMargin; // 间距 } - (void)prepare { [super prepare]; self.contentViewY = 1000; } - (void)setupSubviews { [super setupSubviews]; _contentMaxWidth = self.xtfei_width - 30.f; _contentWidth = 0; _contentHeight = 0; _subViweMargin = self.subViewMargin ? self.subViewMargin : kSubViewMargin; // 占位图片 UIImage *image = [UIImage imageNamed:self.image]; if (image) { [self setupPromptImageView:image]; } else { if (_promptImageView) { [_promptImageView removeFromSuperview]; } } // 标题 if (self.title.length) { [self setupTitleLabel:self.title]; } else { if (_titleLabel) { [_titleLabel removeFromSuperview]; } } // 详细描述 if (self.detail.length) { [self setupDetailLabel:self.detail]; } else { if (_detailLabel) { [_detailLabel removeFromSuperview]; } } // 按钮 if (self.btnTitle.length) { if (self.actionBtnTarget && self.actionBtnAction) { [self setupActionBtn:self.btnTitle target:self.actionBtnTarget action:self.actionBtnAction btnClickBlock:nil]; } else if (self.btnClickBlock) { [self setupActionBtn:self.btnTitle target:nil action:nil btnClickBlock:self.btnClickBlock]; } else { if (_actionButton) { [_actionButton removeFromSuperview]; } } } else { if (_actionButton) { [_actionButton removeFromSuperview]; } } // 自定义view if (self.customView) { _contentWidth = self.customView.xtfei_width; _contentHeight = self.customView.xtfei_y + self.customView.xtfei_height; } // 设置frame [self setSubViewFrame]; } - (void)setSubViewFrame { CGFloat scrollViewWidth = self.bounds.size.width; CGFloat scrollViewHeight = self.bounds.size.height; self.xtfei_size = CGSizeMake(_contentWidth, _contentHeight); CGFloat emptyViewCenterX = scrollViewWidth * 0.5f; CGFloat emptyViewCenterY = scrollViewHeight * 0.5f; self.center = CGPointMake(emptyViewCenterX, emptyViewCenterY); self.contentView.frame = self.bounds; CGFloat centerX = self.contentView.xtfei_width * 0.5f; if (self.customView) { self.customView.xtfei_centerX = centerX; } else { _promptImageView.xtfei_centerX = centerX; _titleLabel.xtfei_centerX = centerX; _detailLabel.xtfei_centerX = centerX; _actionButton.xtfei_centerX = centerX; } if (self.contentViewOffset) { self.xtfei_centerY += self.contentViewOffset; } if (self.contentViewY < 1000) { self.xtfei_y = self.contentViewY; } } - (void)setupPromptImageView:(UIImage *)img { self.promptImageView.image = img; CGFloat imgViewWidth = img.size.width; CGFloat imgViewHeight = img.size.height; self.promptImageView.image = [img imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]; if (self.imageSize.width && self.imageSize.height) { if (imgViewWidth > imgViewHeight) { imgViewHeight = (imgViewHeight / imgViewWidth) * self.imageSize.width; imgViewWidth = self.imageSize.width; } else { imgViewWidth = (imgViewWidth / imgViewHeight) * self.imageSize.height; imgViewHeight = self.imageSize.height; } } self.promptImageView.frame = CGRectMake(0, 0, imgViewWidth, imgViewHeight); _contentWidth = self.promptImageView.xtfei_size.width; _contentHeight = self.promptImageView.xtfei_y + self.promptImageView.xtfei_height; } - (void)setupTitleLabel:(NSString *)titleStr { UIFont *font = self.titleLabFont.pointSize ? self.titleLabFont : kTitleLabFont; CGFloat fontSize = [TFViewHelper getDynamicHeightWithLabelFont:font labelWidth:SCREEN_WIDTH labelText:titleStr]; UIColor *textColor = self.titleLabTextColor ? self.titleLabTextColor : kBlackColor; CGFloat width = [self returnTextWidth:titleStr size:CGSizeMake(_contentMaxWidth, fontSize) font:font].width; self.titleLabel.frame = CGRectMake(0, _contentHeight + _subViweMargin, width, fontSize); self.titleLabel.font = font; self.titleLabel.text = titleStr; self.titleLabel.textColor = textColor; _contentWidth = width > _contentWidth ? width : _contentWidth; _contentHeight = self.titleLabel.xtfei_y + self.titleLabel.xtfei_height; } - (void)setupDetailLabel:(NSString *)detailStr { UIColor *textColor = self.detailLabTextColor ? self.detailLabTextColor : kGrayColor; UIFont *font = self.detailLabFont.pointSize ? self.detailLabFont : kDetailLabFont; CGFloat fontSize = font.pointSize; CGFloat maxHeight = self.detailLabMaxLines ? self.detailLabMaxLines * (fontSize + 5) : 2 * (fontSize + 5); CGSize size = [self returnTextWidth:detailStr size:CGSizeMake(_contentMaxWidth, maxHeight) font:font]; CGFloat width = size.width; CGFloat height = size.height; self.detailLabel.font = font; self.detailLabel.frame = CGRectMake(0, _contentHeight + _subViweMargin, width, height); self.detailLabel.text = detailStr; self.detailLabel.textColor = textColor; _contentWidth = width > _contentWidth ? width : _contentWidth; _contentHeight = self.detailLabel.xtfei_y + self.detailLabel.xtfei_height; } - (void)setupActionBtn:(NSString *)btnTitle target:(id)target action:(SEL)action btnClickBlock:(TFActionTapBlock)btnClickBlock { UIFont *font = self.actionBtnFont.pointSize ? self.actionBtnFont : kActionBtnFont; CGFloat fontSize = font.pointSize; UIColor *titleColor = self.actionBtnTitleColor ? self.actionBtnTitleColor : kBlackColor; UIColor *backGColor = self.actionBtnBackGroundColor ? self.actionBtnBackGroundColor : [UIColor whiteColor]; UIColor *borderColor = self.actionBtnBorderColor ? self.actionBtnBorderColor : [UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1]; CGFloat borderWidth = self.actionBtnBorderWidth ? self.actionBtnBorderWidth : 0.0f; CGFloat cornerRadius = self.actionBtnCornerRadius ? self.actionBtnCornerRadius : 5.f; CGFloat horiMargin = self.actionBtnHorizontalMargin ? self.actionBtnHorizontalMargin : kActionBtnHorizontalMargin; CGFloat height = self.actionBtnHeight ? self.actionBtnHeight : kActionBtnHeight; CGSize textSize = [self returnTextWidth:btnTitle size:CGSizeMake(_contentMaxWidth, fontSize) font:font]; if (height < textSize.height) { height = textSize.height + 4; } // 按钮的宽高 CGFloat btnWidth = textSize.width + horiMargin * 2; CGFloat btnHeight = height; btnWidth = btnWidth > _contentMaxWidth ? _contentMaxWidth : btnWidth; self.actionButton.frame = CGRectMake(0, _contentHeight + _subViweMargin, btnWidth, btnHeight); [self.actionButton setTitle:btnTitle forState:UIControlStateNormal]; self.actionButton.titleLabel.font = font; self.actionButton.backgroundColor = backGColor; [self.actionButton setTitleColor:titleColor forState:UIControlStateNormal]; self.actionButton.layer.borderColor = borderColor.CGColor; self.actionButton.layer.borderWidth = borderWidth; self.actionButton.layer.cornerRadius = cornerRadius; // 添加事件 if (target && action) { [self.actionButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [self.actionButton addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } else if (btnClickBlock) { [self.actionButton addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } _contentWidth = btnWidth > _contentWidth ? btnWidth : _contentWidth; _contentHeight = self.actionButton.xtfei_y + self.actionButton.xtfei_height; } - (UIImageView *)promptImageView { if (!_promptImageView) { _promptImageView = [[UIImageView alloc] init]; _promptImageView.contentMode = UIViewContentModeScaleAspectFit; [self.contentView addSubview:_promptImageView]; } return _promptImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_titleLabel]; } return _titleLabel; } - (UILabel *)detailLabel { if (!_detailLabel) { _detailLabel = [[UILabel alloc] init]; _detailLabel.textAlignment = NSTextAlignmentCenter; _detailLabel.numberOfLines = 0; [self.contentView addSubview:_detailLabel]; } return _detailLabel; } - (UIButton *)actionButton { if (!_actionButton) { _actionButton = [[UIButton alloc] init]; _actionButton.layer.masksToBounds = YES; [self.contentView addSubview:_actionButton]; } return _actionButton; } - (void)setSubViewMargin:(CGFloat)subViewMargin { if (_subViewMargin != subViewMargin) { _subViewMargin = subViewMargin; if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) { [self setupSubviews]; } } } - (void)setContentViewOffset:(CGFloat)contentViewOffset { if (_contentViewOffset != contentViewOffset) { _contentViewOffset = contentViewOffset; if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) { self.xtfei_centerY += self.contentViewOffset; } } } - (void)setContentViewY:(CGFloat)contentViewY { if (_contentViewY != contentViewY) { _contentViewY = contentViewY; if (_promptImageView || _titleLabel || _detailLabel || _actionButton || self.customView) { self.xtfei_y = self.contentViewY; } } } - (void)setImageSize:(CGSize)imageSize { if (_imageSize.width != imageSize.width || _imageSize.height != imageSize.height) { _imageSize = imageSize; if (_promptImageView) { [self setupSubviews]; } } } - (void)setTitleLabFont:(UIFont *)titleLabFont { if (_titleLabFont != titleLabFont) { _titleLabFont = titleLabFont; if (_titleLabel) { [self setupSubviews]; } } } - (void)setTitleLabTextColor:(UIColor *)titleLabTextColor { if (_titleLabTextColor != titleLabTextColor) { _titleLabTextColor = titleLabTextColor; if (_titleLabel) { _titleLabel.textColor = titleLabTextColor; } } } - (void)setDetailLabFont:(UIFont *)detailLabFont { if (_detailLabFont != detailLabFont) { _detailLabFont = detailLabFont; if (_detailLabel) { [self setupSubviews]; } } } - (void)setDetailLabMaxLines:(NSInteger)detailLabMaxLines { if (_detailLabMaxLines != detailLabMaxLines) { _detailLabMaxLines = detailLabMaxLines; if (_detailLabel) { [self setupSubviews]; } } } - (void)setDetailLabTextColor:(UIColor *)detailLabTextColor { if (_detailLabTextColor != detailLabTextColor) { _detailLabTextColor = detailLabTextColor; if (_detailLabel) { _detailLabel.textColor = detailLabTextColor; } } } - (void)setActionBtnFont:(UIFont *)actionBtnFont { if (_actionBtnFont != actionBtnFont) { _actionBtnFont = actionBtnFont; if (_actionButton) { [self setupSubviews]; } } } - (void)setActionBtnHeight:(CGFloat)actionBtnHeight { if (_actionBtnHeight != actionBtnHeight) { _actionBtnHeight = actionBtnHeight; if (_actionButton) { [self setupSubviews]; } } } - (void)setActionBtnHorizontalMargin:(CGFloat)actionBtnHorizontalMargin { if (_actionBtnHorizontalMargin != actionBtnHorizontalMargin) { _actionBtnHorizontalMargin = actionBtnHorizontalMargin; if (_actionButton) { [self setupSubviews]; } } } - (void)setActionBtnCornerRadius:(CGFloat)actionBtnCornerRadius { if (_actionBtnCornerRadius != actionBtnCornerRadius) { _actionBtnCornerRadius = actionBtnCornerRadius; if (_actionButton) { _actionButton.layer.cornerRadius = actionBtnCornerRadius; } } } - (void)setActionBtnBorderWidth:(CGFloat)actionBtnBorderWidth { if (actionBtnBorderWidth != _actionBtnBorderWidth) { _actionBtnBorderWidth = actionBtnBorderWidth; if (_actionButton) { _actionButton.layer.borderWidth = actionBtnBorderWidth; } } } - (void)setActionBtnBorderColor:(UIColor *)actionBtnBorderColor { if (_actionBtnBorderColor != actionBtnBorderColor) { _actionBtnBorderColor = actionBtnBorderColor; if (_actionButton) { _actionButton.layer.borderColor = actionBtnBorderColor.CGColor; } } } - (void)setActionBtnTitleColor:(UIColor *)actionBtnTitleColor { if (_actionBtnTitleColor != actionBtnTitleColor) { _actionBtnTitleColor = actionBtnTitleColor; if (_actionButton) { [_actionButton setTitleColor:actionBtnTitleColor forState:UIControlStateNormal]; } } } - (void)setActionBtnBackGroundColor:(UIColor *)actionBtnBackGroundColor { if (actionBtnBackGroundColor != _actionBtnBackGroundColor) { _actionBtnBackGroundColor = actionBtnBackGroundColor; if (_actionButton) { [_actionButton setBackgroundColor:actionBtnBackGroundColor]; } } } - (void)actionBtnClick:(UIButton *)sender { if (self.btnClickBlock) { self.btnClickBlock(); } } - (CGSize)returnTextWidth:(NSString *)text size:(CGSize)size font:(UIFont *)font { return [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size; } @end