// Created by lee on 2019/5/20. // Copyright © 2023 YUMI. All rights reserved. #import "TTAlertView.h" #import "TTAlertConfig.h" #import "DJDKMIMOMColor.h" #import static CGFloat const kMargin = 25.f; static CGFloat const kPadding = 20.f; static CGFloat const kBtnHeight = 38.f; @interface TTAlertView () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *messageLabel; @property (nonatomic, strong) UIButton *cancelButton; @property (nonatomic, strong) UIButton *confirmButton; @property (nonatomic, strong) UIStackView *stackView; @end @implementation TTAlertView #pragma mark - lifeCyle - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initViews]; [self initConstraints]; } return self; } - (void)initViews { [self addSubview:self.titleLabel]; [self addSubview:self.messageLabel]; [self addSubview:self.stackView]; [self.stackView addSubview:self.cancelButton]; [self.stackView addSubview:self.confirmButton]; } - (void)initConstraints { [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.top.mas_equalTo(kPadding); make.leading.trailing.mas_equalTo(self).inset(kPadding); }]; [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(kMargin); make.leading.trailing.mas_equalTo(self).inset(kPadding); make.bottom.mas_equalTo(self).offset(-kBtnHeight * 2); }]; [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-kPadding); make.centerX.mas_equalTo(self); }]; [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(kBtnHeight); make.width.mas_equalTo(self.mas_width).multipliedBy(0.4); }]; [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(kBtnHeight); make.width.mas_equalTo(self.mas_width).multipliedBy(0.4); }]; } #pragma mark - Button Events - (void)onClickConfirmButtonAction:(UIButton *)confirmButton { !_confirmAction ?: _confirmAction(); !_dismissAction ?: _dismissAction(); } - (void)onClickCancelButtonAction:(UIButton *)cancelButton { !_cancelAction ?: _cancelAction(); !_dismissAction ?: _dismissAction(); } #pragma mark - private method - (NSMutableAttributedString *)messageAttributeString:(TTAlertConfig *)config { NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:config.message]; if (config.messageLineSpacing > 0) { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = config.messageLineSpacing; paragraphStyle.alignment = config.messageTextAlignment; [attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, config.message.length)]; } [config.messageAttributedConfig enumerateObjectsUsingBlock:^(TTAlertMessageAttributedConfig * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[TTAlertMessageAttributedConfig class]]) { if (obj.text && obj.text.length > 0) { NSRange range = [config.message rangeOfString:obj.text]; if (obj.range.length != 0) { if (obj.range.location + obj.range.length > config.message.length) { NSAssert(NO, @"obj.range out of bounds"); return; } range = obj.range; } if (obj.font) { [attString addAttribute:NSFontAttributeName value:obj.font range:range]; } if (obj.color) { [attString addAttribute:NSForegroundColorAttributeName value:obj.color range:range]; } } } }]; return attString; } #pragma mark - getter && setter - (void)setConfig:(TTAlertConfig *)config { _config = config; if (config.cornerRadius > 0) { self.layer.cornerRadius = config.cornerRadius; self.layer.masksToBounds = YES; } self.backgroundColor = config.backgroundColor; _titleLabel.text = config.title; _titleLabel.textColor = config.titleColor; _titleLabel.font = config.titleFont; _cancelButton.hidden = config.actionStyle == TTAlertActionConfirmStyle; _confirmButton.hidden = config.actionStyle == TTAlertActionCancelStyle; [_cancelButton setTitle:config.cancelButtonConfig.title forState:UIControlStateNormal]; [_cancelButton setTitleColor:config.cancelButtonConfig.titleColor forState:UIControlStateNormal]; [_cancelButton.titleLabel setFont:config.cancelButtonConfig.font]; [_cancelButton setBackgroundColor:config.cancelButtonConfig.backgroundColor]; [_cancelButton setBackgroundImage:config.cancelButtonConfig.backgroundImage forState:UIControlStateNormal]; if (config.cancelButtonConfig.cornerRadius > 0) { _cancelButton.layer.cornerRadius = config.cancelButtonConfig.cornerRadius; _cancelButton.layer.masksToBounds = YES; } [_confirmButton setTitle:config.confirmButtonConfig.title forState:UIControlStateNormal]; [_confirmButton setTitleColor:config.confirmButtonConfig.titleColor forState:UIControlStateNormal]; [_confirmButton.titleLabel setFont:config.confirmButtonConfig.font]; [_confirmButton setBackgroundColor:config.confirmButtonConfig.backgroundColor]; [_confirmButton setBackgroundImage:config.confirmButtonConfig.backgroundImage forState:UIControlStateNormal]; if (config.confirmButtonConfig.cornerRadius > 0) { _confirmButton.layer.cornerRadius = config.confirmButtonConfig.cornerRadius; _confirmButton.layer.masksToBounds = YES; } _messageLabel.font = config.messageFont; _messageLabel.textColor = config.messageColor; if (config.messageAttributedConfig.count > 0) { _messageLabel.attributedText = [self messageAttributeString:config]; _messageLabel.textAlignment = NSTextAlignmentCenter; } else if(config.messageAttributed.length > 0) { _messageLabel.attributedText = config.messageAttributed; _messageLabel.textAlignment = NSTextAlignmentCenter; } else { _messageLabel.text = config.message; _messageLabel.textAlignment = config.messageTextAlignment; } _cancelButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _cancelButton.layer.borderWidth = 2.f; _confirmButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _confirmButton.layer.borderWidth = 2.f; } - (void)setIsConfigBoard:(BOOL)isConfigBoard { _isConfigBoard = isConfigBoard; if (isConfigBoard) { _cancelButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _cancelButton.layer.borderWidth = 2.f; _confirmButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _confirmButton.layer.borderWidth = 2.f; }else { _cancelButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _cancelButton.layer.borderWidth = 0; _confirmButton.layer.borderColor = [DJDKMIMOMColor dividerColor].CGColor; _confirmButton.layer.borderWidth = 0; } } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 0; } return _titleLabel ; } - (UILabel *)messageLabel { if (!_messageLabel) { _messageLabel = [[UILabel alloc] init]; _messageLabel.numberOfLines = 0; _messageLabel.textAlignment = NSTextAlignmentCenter; _messageLabel.minimumScaleFactor = 0.7; _messageLabel.adjustsFontSizeToFitWidth = YES; } return _messageLabel; } - (UIButton *)cancelButton { if (!_cancelButton) { _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_cancelButton addTarget:self action:@selector(onClickCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (UIButton *)confirmButton { if (!_confirmButton) { _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_confirmButton addTarget:self action:@selector(onClickConfirmButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _confirmButton; } - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.cancelButton, self.confirmButton]]; _stackView.distribution = UIStackViewDistributionFillEqually; _stackView.alignment = UIStackViewAlignmentCenter; _stackView.spacing = 16; } return _stackView; } @end