You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
395 lines
12 KiB
395 lines
12 KiB
4 years ago
|
//
|
||
|
// TFAlertView.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/17.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFAlertView.h"
|
||
|
|
||
|
@interface TFAlertView ()<UIGestureRecognizerDelegate>
|
||
|
|
||
|
@property (nonatomic ,strong) UITapGestureRecognizer *dismissTapGesture;
|
||
|
@property (nonatomic ,assign) CGFloat alertViewBtnWidth;
|
||
|
@property (nonatomic ,assign) BOOL inController;
|
||
|
|
||
|
@end
|
||
|
|
||
|
@implementation TFAlertView
|
||
|
|
||
|
- (instancetype)init
|
||
|
{
|
||
|
if (self = [super init]) {
|
||
|
|
||
|
_inController = NO;
|
||
|
[self initialize];
|
||
|
[self createSubviews];
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (instancetype)initInController
|
||
|
{
|
||
|
if (self = [super init]) {
|
||
|
|
||
|
_inController = YES;
|
||
|
[self initialize];
|
||
|
[self createSubviews];
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (void)initialize
|
||
|
{
|
||
|
_showDivider = YES;
|
||
|
_alertViewBtnHeight = 50.0f;
|
||
|
_alertViewWidth = SCREEN_WIDTH - 3 * kMargin;
|
||
|
self.alertBtnType = TFAlertButtonTypeDouble;
|
||
|
self.alertDisappearType = TFAlertViewDisappearTypeNormal;
|
||
|
|
||
|
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||
|
self.backgroundColor = kBlackTransparentColor;
|
||
|
if (_inController) {
|
||
|
[kMainWindow.rootViewController.view addSubview:self];
|
||
|
} else {
|
||
|
[kMainWindow addSubview:self];
|
||
|
}
|
||
|
if (!self.superview) {
|
||
|
UIViewController *vc = [UIApplication sharedApplication].windows.firstObject.rootViewController;
|
||
|
[vc.view addSubview:self];
|
||
|
}
|
||
|
|
||
|
self.dismissTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAlertView)];
|
||
|
self.dismissTapGesture.numberOfTapsRequired = 1;
|
||
|
self.dismissTapGesture.delegate = self;
|
||
|
[self addGestureRecognizer:self.dismissTapGesture];
|
||
|
}
|
||
|
|
||
|
- (void)createSubviews
|
||
|
{
|
||
|
// 添加背景
|
||
|
[self addSubview:self.alertBackView];
|
||
|
// 添加关闭按钮
|
||
|
[self.alertBackView addSubview:self.closeButton];
|
||
|
// 添加标题
|
||
|
[self.alertBackView addSubview:self.alertTitleLabel];
|
||
|
// 添加内容
|
||
|
[self.alertBackView addSubview:self.contentScrollView];
|
||
|
|
||
|
[self.contentScrollView addSubview:self.contentLabel];
|
||
|
// 取消按钮
|
||
|
[self.alertBackView addSubview:self.cancelButton];
|
||
|
// 确认按钮
|
||
|
[self.alertBackView addSubview:self.confirmButton];
|
||
|
}
|
||
|
|
||
|
// 显示弹框
|
||
|
- (void)showAlertView
|
||
|
{
|
||
|
[self.alertBackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.width.mas_equalTo(self.alertViewWidth);
|
||
|
make.bottom.mas_equalTo(self.confirmButton.mas_bottom).with.offset(0);
|
||
|
make.centerX.mas_equalTo(self.mas_centerX);
|
||
|
make.centerY.mas_equalTo(self.mas_centerY).with.offset(0);
|
||
|
}];
|
||
|
|
||
|
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.top.mas_equalTo(self.alertBackView.mas_top);
|
||
|
make.right.mas_equalTo(self.alertBackView.mas_right);
|
||
|
make.width.height.mas_equalTo(50);
|
||
|
}];
|
||
|
|
||
|
[self.alertTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.mas_equalTo(self.alertBackView.mas_left).with.offset(kMargin);
|
||
|
make.right.mas_equalTo(self.alertBackView.mas_right).with.offset(- kMargin);
|
||
|
make.top.mas_equalTo(self.alertBackView.mas_top).with.offset(kMargin);
|
||
|
if (self.alertTitle.length > 0) {
|
||
|
make.height.mas_equalTo(30);
|
||
|
} else {
|
||
|
make.height.mas_equalTo(CGFLOAT_MIN);
|
||
|
}
|
||
|
}];
|
||
|
|
||
|
[self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.centerX.mas_equalTo(self.alertTitleLabel.centerX);
|
||
|
make.top.mas_equalTo(self.alertTitleLabel.mas_bottom);
|
||
|
make.width.mas_equalTo(self.alertViewWidth - 2 * kMargin);
|
||
|
make.height.mas_equalTo([self getContentScrollViewHeight]);
|
||
|
}];
|
||
|
|
||
|
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.top.mas_equalTo(0);
|
||
|
make.width.mas_equalTo(self.contentScrollView.mas_width);
|
||
|
make.height.mas_equalTo([self getContentLabelHeight]);
|
||
|
}];
|
||
|
|
||
|
|
||
|
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.mas_equalTo(self.alertBackView.mas_left).with.offset(0);
|
||
|
make.top.mas_equalTo(self.contentScrollView.mas_bottom).with.offset(kMargin);
|
||
|
make.height.mas_equalTo(_alertViewBtnHeight);
|
||
|
if (self.alertBtnType == TFAlertButtonTypeSingleConfirm) {
|
||
|
make.width.mas_equalTo(CGFLOAT_MIN);
|
||
|
} else {
|
||
|
make.width.mas_equalTo(self.alertViewBtnWidth);
|
||
|
}
|
||
|
}];
|
||
|
|
||
|
|
||
|
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.right.mas_equalTo(self.alertBackView.mas_right).with.offset(0);
|
||
|
make.top.mas_equalTo(self.contentScrollView.mas_bottom).with.offset(kMargin);
|
||
|
make.height.mas_equalTo(_alertViewBtnHeight);
|
||
|
if (self.alertBtnType == TFAlertButtonTypeSingleCancel) {
|
||
|
make.width.mas_equalTo(CGFLOAT_MIN);
|
||
|
} else {
|
||
|
make.width.mas_equalTo(self.alertViewBtnWidth);
|
||
|
}
|
||
|
}];
|
||
|
|
||
|
if (self.alertBtnType != TFAlertButtonTypeNone && self.showDivider) {
|
||
|
UIView *line = [[UIView alloc] init];
|
||
|
line.backgroundColor = kGrayLineColor;
|
||
|
[self.alertBackView addSubview:line];
|
||
|
|
||
|
[line mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.mas_equalTo(self.alertBackView.mas_left);
|
||
|
make.right.mas_equalTo(self.alertBackView.mas_right);
|
||
|
make.height.mas_equalTo(kCellLineHeight + 0.1f);
|
||
|
make.bottom.mas_equalTo(self.alertBackView.mas_bottom).with.offset(- _alertViewBtnHeight);
|
||
|
}];
|
||
|
|
||
|
if (self.alertBtnType == TFAlertButtonTypeDouble) {
|
||
|
UIView *line = [[UIView alloc] init];
|
||
|
line.backgroundColor = kGrayLineColor;
|
||
|
[self.alertBackView addSubview:line];
|
||
|
|
||
|
[line mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.left.mas_equalTo(self.confirmButton.mas_left);
|
||
|
make.top.mas_equalTo(self.confirmButton.mas_top);
|
||
|
make.height.mas_equalTo(self.confirmButton.mas_height);
|
||
|
make.width.mas_equalTo(kCellLineHeight + 0.1f);
|
||
|
}];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 关闭弹框
|
||
|
- (void)closeAlertView
|
||
|
{
|
||
|
[self removeFromSuperview];
|
||
|
}
|
||
|
|
||
|
- (CGFloat)getContentLabelHeight
|
||
|
{
|
||
|
if (self.attributedStr.length > 0) {
|
||
|
return [TFViewHelper getDynamicHeightWithLabelFont:kMainFont labelWidth:SCREEN_WIDTH - 3 * kMargin labelText:self.attributedStr.string] + 2 * kMargin;
|
||
|
} else if (self.alertDetailContent.length > 0) {
|
||
|
return [TFViewHelper getDynamicHeightWithLabelFont:kMainFont labelWidth:SCREEN_WIDTH - 3 * kMargin labelText:self.alertDetailContent] + kMargin;
|
||
|
}
|
||
|
return CGFLOAT_MIN;
|
||
|
}
|
||
|
|
||
|
- (CGFloat)getContentScrollViewHeight
|
||
|
{
|
||
|
if ([self getContentLabelHeight] > (SCREEN_HEIGHT / 3)) {
|
||
|
[self.contentScrollView setContentSize:CGSizeMake(0, [self getContentLabelHeight])];
|
||
|
return SCREEN_HEIGHT / 3;
|
||
|
}
|
||
|
return [self getContentLabelHeight];
|
||
|
}
|
||
|
|
||
|
//- (NSString *)alertTitle
|
||
|
//{
|
||
|
// if (!_alertTitle) {
|
||
|
// return TFLocalizedString(@"提示");
|
||
|
// }
|
||
|
// return _alertTitle;
|
||
|
//}
|
||
|
|
||
|
- (void)setAlertTitle:(NSString *)alertTitle
|
||
|
{
|
||
|
_alertTitle = alertTitle;
|
||
|
|
||
|
self.alertTitleLabel.text = alertTitle;
|
||
|
}
|
||
|
|
||
|
- (void)setAlertDetailContent:(NSString *)alertDetailContent
|
||
|
{
|
||
|
_alertDetailContent = alertDetailContent;
|
||
|
|
||
|
self.contentLabel.text = alertDetailContent;
|
||
|
}
|
||
|
|
||
|
- (void)setAttributedStr:(NSMutableAttributedString *)attributedStr
|
||
|
{
|
||
|
_attributedStr = attributedStr;
|
||
|
|
||
|
self.contentLabel.attributedText = attributedStr;
|
||
|
}
|
||
|
|
||
|
- (void)setCancelTitle:(NSString *)cancelTitle
|
||
|
{
|
||
|
[self.cancelButton setTitle:cancelTitle forState:UIControlStateNormal];
|
||
|
}
|
||
|
|
||
|
- (void)setConfirmTitle:(NSString *)confirmTitle
|
||
|
{
|
||
|
[self.confirmButton setTitle:confirmTitle forState:UIControlStateNormal];
|
||
|
}
|
||
|
|
||
|
- (void)setAlertBtnType:(TFAlertButtonType)alertBtnType
|
||
|
{
|
||
|
_alertBtnType = alertBtnType;
|
||
|
|
||
|
switch (alertBtnType) {
|
||
|
case TFAlertButtonTypeNone:
|
||
|
self.alertViewBtnWidth = CGFLOAT_MIN;
|
||
|
_alertViewBtnHeight = CGFLOAT_MIN;
|
||
|
break;
|
||
|
case TFAlertButtonTypeSingleConfirm:
|
||
|
case TFAlertButtonTypeSingleCancel:
|
||
|
self.alertViewBtnWidth = self.alertViewWidth;
|
||
|
_alertViewBtnHeight = 44.0f;
|
||
|
break;
|
||
|
case TFAlertButtonTypeDouble:
|
||
|
self.alertViewBtnWidth = (self.alertViewWidth) / 2;
|
||
|
_alertViewBtnHeight = 44.0f;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)setAlertDisappearType:(TFAlertViewDisappearType)alertDisappearType
|
||
|
{
|
||
|
_alertDisappearType = alertDisappearType;
|
||
|
|
||
|
if (alertDisappearType != TFAlertViewDisappearTypeNormal) {
|
||
|
[self removeGestureRecognizer:self.dismissTapGesture];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#pragma mark - lazy
|
||
|
- (UIView *)alertBackView
|
||
|
{
|
||
|
if (!_alertBackView) {
|
||
|
_alertBackView = [[UIView alloc] init];
|
||
|
_alertBackView.backgroundColor = [UIColor whiteColor];
|
||
|
_alertBackView.layer.cornerRadius = 8;
|
||
|
[self addSubview:_alertBackView];
|
||
|
}
|
||
|
return _alertBackView;
|
||
|
}
|
||
|
|
||
|
- (UIButton *)closeButton
|
||
|
{
|
||
|
if (!_closeButton) {
|
||
|
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
|
_closeButton.tintColor = kGrayTextLightColor;
|
||
|
[_closeButton setImageEdgeInsets:UIEdgeInsetsMake(15, 15, 15, 15)];
|
||
|
[_closeButton setImage:[[UIImage imageNamed:@"public_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
|
||
|
[_closeButton addTarget:self action:@selector(closeAlertView) forControlEvents:UIControlEventTouchUpInside];
|
||
|
}
|
||
|
return _closeButton;
|
||
|
}
|
||
|
|
||
|
- (UILabel *)alertTitleLabel
|
||
|
{
|
||
|
if (!_alertTitleLabel) {
|
||
|
_alertTitleLabel = [[UILabel alloc] init];
|
||
|
_alertTitleLabel.text = self.alertTitle;
|
||
|
_alertTitleLabel.backgroundColor = [UIColor clearColor];
|
||
|
_alertTitleLabel.font = kBoldFont16;
|
||
|
_alertTitleLabel.textColor = kBlackColor;
|
||
|
_alertTitleLabel.textAlignment = NSTextAlignmentCenter;
|
||
|
_alertTitleLabel.numberOfLines = 1;
|
||
|
}
|
||
|
return _alertTitleLabel;
|
||
|
}
|
||
|
|
||
|
- (UIScrollView *)contentScrollView
|
||
|
{
|
||
|
if (!_contentScrollView) {
|
||
|
_contentScrollView = [[UIScrollView alloc] init];
|
||
|
_contentScrollView.showsVerticalScrollIndicator = NO;
|
||
|
_contentScrollView.showsHorizontalScrollIndicator = NO;
|
||
|
}
|
||
|
return _contentScrollView;
|
||
|
}
|
||
|
|
||
|
- (YYLabel *)contentLabel
|
||
|
{
|
||
|
if (!_contentLabel) {
|
||
|
_contentLabel = [[YYLabel alloc] init];
|
||
|
_contentLabel.numberOfLines = 0;
|
||
|
_contentLabel.font = kMainFont;
|
||
|
_contentLabel.textColor = kGrayTextLightColor;
|
||
|
_contentLabel.textAlignment = NSTextAlignmentCenter;
|
||
|
}
|
||
|
return _contentLabel;
|
||
|
}
|
||
|
|
||
|
- (UIButton *)confirmButton
|
||
|
{
|
||
|
if (!_confirmButton) {
|
||
|
_confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
|
_confirmButton.backgroundColor = kWhiteColor;
|
||
|
_confirmButton.layer.cornerRadius = 8;
|
||
|
[_confirmButton.titleLabel setFont:kMainFont];
|
||
|
[_confirmButton setTitle:TFLocalizedString(@"确定") forState:UIControlStateNormal];
|
||
|
[_confirmButton setTitleColor:kMainColor forState:UIControlStateNormal];
|
||
|
[_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
|
||
|
}
|
||
|
return _confirmButton;
|
||
|
}
|
||
|
|
||
|
- (UIButton *)cancelButton
|
||
|
{
|
||
|
if (!_cancelButton) {
|
||
|
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
|
_cancelButton.backgroundColor = kWhiteColor;
|
||
|
_cancelButton.layer.cornerRadius = 8;
|
||
|
[_cancelButton.titleLabel setFont:kMainFont];
|
||
|
[_cancelButton setTitle:TFLocalizedString(@"取消") forState:UIControlStateNormal];
|
||
|
[_cancelButton setTitleColor:kBlackColor forState:UIControlStateNormal];
|
||
|
[_cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
|
||
|
}
|
||
|
return _cancelButton;
|
||
|
}
|
||
|
|
||
|
#pragma mark - action
|
||
|
- (void)cancelButtonClick
|
||
|
{
|
||
|
if (self.cancelButtonClickBlock) {
|
||
|
self.cancelButtonClickBlock();
|
||
|
}
|
||
|
[self closeAlertView];
|
||
|
}
|
||
|
|
||
|
- (void)confirmButtonClick
|
||
|
{
|
||
|
if (self.confirmButtonClickBlock) {
|
||
|
self.confirmButtonClickBlock();
|
||
|
}
|
||
|
|
||
|
if (self.alertDisappearType == TFAlertViewDisappearTypeNever) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
[self closeAlertView];
|
||
|
}
|
||
|
|
||
|
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
|
||
|
{
|
||
|
if ([touch.view isEqual:self.confirmButton] || [touch.view isEqual:self]) {
|
||
|
return YES;
|
||
|
} else {
|
||
|
return NO;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@end
|