2024-06-07 13:38:31 +08:00

307 lines
12 KiB
Objective-C

//
// HBAlertView.m
// QinJiuTV
//
// Created by HI_LOSER on 2024/5/16.
//
#import "HBAlertView.h"
#import <Masonry/Masonry.h>
typedef enum : NSUInteger {
ClassRoomAlertViewCancel,
ClassRoomAlertViewConfirm,
} ClassRoomAlertViewActionTag;
#define AWidth 320
#define AHeight 134
@interface HBAlertView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *downgradeButton;
@property (nonatomic, strong) UIView *verticalLine;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSString *info;
@property (nonatomic, strong) NSString *highlightText;
@property (nonatomic, strong) NSString *leftTitle;
@property (nonatomic, strong) NSString *rightTitle;
@property (nonatomic, copy) ButtonBlock cancel;
@property (nonatomic, copy) ButtonBlock confirm;
@end
@implementation HBAlertView
+ (void)showMessage:(NSString *)message
leftTitle:(NSString *)leftTitle
rightTitle:(NSString *)rightTitle
leftBlock:(ButtonBlock)cancel
rightBlock:(ButtonBlock)confirm {
[self showAlertWithTitle:@""
message:message
highlightText:@""
describeTitle:@""
leftTitle:leftTitle
rightTitle:rightTitle
cancel:cancel
confirm:confirm];
}
+ (void)showAlertWithTitle:(NSString *)title
message:(NSString *)message
describeTitle:(NSString *)describeTitle
confirmTitle:(NSString *)confirmTitle
confirm:(ButtonBlock)confirm {
[self showAlertWithTitle:title
message:message
highlightText:@""
describeTitle:describeTitle
leftTitle:@""
rightTitle:confirmTitle
cancel:nil
confirm:confirm];
}
+ (void)showAlertWithMessage:(NSString *)Message
highlightText:(NSString *)highlightText
leftTitle:(NSString *)leftTitle
rightTitle:(NSString *)rightTitle
cancel:(ButtonBlock)cancel
confirm:(ButtonBlock)confirm {
[self showAlertWithTitle:@""
message:Message
highlightText:highlightText
describeTitle:@""
leftTitle:leftTitle
rightTitle:rightTitle
cancel:cancel
confirm:confirm];
}
+ (void)showAlertWithTitle:(NSString *)title
message:(NSString *)message
highlightText:(NSString *)highlightText
describeTitle:(NSString *)describeTitle
leftTitle:(NSString *)leftTitle
rightTitle:(NSString *)rightTitle
cancel:(ButtonBlock)cancel
confirm:(ButtonBlock)confirm {
HBAlertView *alertView = [[HBAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertView.title = title;
alertView.message = message;
alertView.info = describeTitle;
alertView.highlightText = highlightText;
alertView.leftTitle = leftTitle;
alertView.rightTitle = rightTitle;
alertView.cancel = cancel;
alertView.confirm = confirm;
[alertView addSubviews];
[alertView showAlertView];
}
- (void)acs_radiusWithView:(UIView *)view radius:(CGFloat)radius corner:(UIRectCorner)corner {
if (@available(iOS 11.0, *)) {
view.layer.cornerRadius = radius;
view.layer.maskedCorners = (CACornerMask)corner;
} else {
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = path.CGPath;
view.layer.mask = maskLayer;
}
if ([self isKindOfClass:[UIImageView class]] || [self isKindOfClass:[UIButton class]]){
self.layer.masksToBounds = YES;
}
}
- (void)showAlertView {
[[UIApplication sharedApplication].windows.firstObject addSubview:self];
}
- (void)dismissAlertView {
[self removeFromSuperview];
}
- (void)addSubviews {
CGRect rect = CGRectMake(([UIScreen mainScreen].bounds.size.width - AWidth) / 2,
([UIScreen mainScreen].bounds.size.height - AHeight) / 2, AWidth, AHeight);
UIView *contentView = [[UIView alloc] initWithFrame:rect];
contentView.backgroundColor = [UIColor colorWithRed:40.0/255 green:42.0/255.0 blue:47.0/255.0 alpha:1.0];
contentView.layer.cornerRadius = 8;
contentView.layer.masksToBounds = YES;
[self addSubview:contentView];
[contentView addSubview:self.titleLabel];
[contentView addSubview:self.messageLabel];
[contentView addSubview:self.infoLabel];
[contentView addSubview:self.cancelButton];
[contentView addSubview:self.downgradeButton];
[self acs_radiusWithView:self.cancelButton radius:6.0 corner:UIRectCornerAllCorners];
[self acs_radiusWithView:self.downgradeButton radius:6.0 corner:UIRectCornerAllCorners];
if (self.title.length > 0) {
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(contentView.mas_top).offset(15);
make.left.equalTo(contentView.mas_left).offset(13);
make.right.equalTo(contentView.mas_right).offset(-13);
make.height.offset(20);
}];
} else {
self.titleLabel.hidden = YES;
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.width.height.left.offset(0);
}];
}
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
make.left.equalTo(contentView.mas_left).offset(13);
make.right.equalTo(contentView.mas_right).offset(-13);
}];
if (self.info.length > 0) {
[self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
make.left.equalTo(contentView.mas_left).offset(13);
make.right.equalTo(contentView.mas_right).offset(-13);
}];
} else {
self.infoLabel.hidden = YES;
[self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
make.width.height.left.offset(0);
}];
}
if (self.leftTitle.length > 0) {
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(contentView).offset(40.0);
make.bottom.equalTo(contentView.mas_bottom).offset(-20.0);
make.height.equalTo(@28.0);
make.width.equalTo(@90.0);
}];
[self.downgradeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(contentView).offset(-40.0);
make.bottom.equalTo(contentView.mas_bottom).offset(-20.0);
make.height.equalTo(@28.0);
make.width.equalTo(@90.0);
}];
} else {
[self.downgradeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(contentView);
make.bottom.equalTo(contentView.mas_bottom).offset(0);
make.height.equalTo(@28.0);
make.width.equalTo(@90.0);
}];
}
[contentView updateConstraintsIfNeeded];
[contentView layoutIfNeeded];
rect.size.height = CGRectGetMaxY(self.infoLabel.frame) + 44 + 10;
rect.origin.y = ([UIScreen mainScreen].bounds.size.height - rect.size.height) / 2;
contentView.frame = rect;
}
- (void)buttonAction:(UIButton *)button {
if (button.tag == ClassRoomAlertViewCancel) {
if (self.cancel) {
self.cancel();
}
} else {
if (self.confirm) {
self.confirm();
}
}
[self dismissAlertView];
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont boldSystemFontOfSize:18];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.text = self.title;
_titleLabel.numberOfLines = 1;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
_titleLabel.textColor = UIColor.whiteColor;
}
return _titleLabel;
}
- (UILabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.text = self.message;
_messageLabel.numberOfLines = 0;
_messageLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
_messageLabel.textColor = UIColor.whiteColor;
if (self.highlightText.length > 0) {
NSRange range = [self.message rangeOfString:self.highlightText];
if (range.location != NSNotFound) {
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:self.message];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithRed:234.0/255.0 green:51.0/255.0 blue:79.0/255.0 alpha:1.0]
range:range]; //0x3a91f3
_messageLabel.attributedText = attributedString;
}
}
}
return _messageLabel;
}
- (UILabel *)infoLabel {
if (!_infoLabel) {
_infoLabel = [[UILabel alloc] init];
_infoLabel.font = [UIFont systemFontOfSize:12.0];
_infoLabel.textAlignment = NSTextAlignmentCenter;
_infoLabel.text = self.info;
_infoLabel.numberOfLines = 0;
_infoLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
_infoLabel.textColor = UIColor.grayColor;
}
return _infoLabel;
}
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [[UIButton alloc] init];
_cancelButton.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:64.0/255.0 blue:69.0/255.0 alpha:1.0];
[_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular]];
[_cancelButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[_cancelButton setTitle:self.leftTitle forState:UIControlStateNormal];
_cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
_cancelButton.tag = ClassRoomAlertViewCancel;
[_cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIButton *)downgradeButton {
if (!_downgradeButton) {
_downgradeButton = [[UIButton alloc] init];
_downgradeButton.backgroundColor = [UIColor colorWithRed:234.0/255.0 green:51.0/255.0 blue:79.0/255.0 alpha:1.0];
[_downgradeButton.titleLabel setFont:[UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular]];
[_downgradeButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[_downgradeButton setTitle:self.rightTitle forState:UIControlStateNormal];
_downgradeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
_downgradeButton.tag = ClassRoomAlertViewConfirm;
[_downgradeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _downgradeButton;
}
@end