项目要求 就自己写了一个弹框 不知道对不对 先记录下来 目前支持 四种样式吧 可以按钮文字 内容的个别文字变色
改变文字颜色的调用方法
SJZAlertViewController *vc = [[SJZAlertViewController alloc] init];
__weak typeof(self) weakSelf = self;
NSString *cardStr = [NSString stringWithFormat:@"确定消耗%@张推荐卡在%@吗?",cardnumStr,cardNameStr];
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;// 这个写里面就好了 不需要这里写
NSMutableArray *rangeArray = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithRange:NSMakeRange(4, cardnumStr.length+1)],[NSValue valueWithRange:NSMakeRange(9+cardnumStr.length, cardNameStr.length+1)], nil];
NSMutableArray *colorArray = [[NSMutableArray alloc] initWithObjects:RGBA(45, 217, 201, 1),RGBA(45, 217, 201, 1), nil];
[vc alerWithButtonConfigurationTitle:@"推荐卡消耗" message:cardStr style:SJZAlertControllerStyleAlert withRange:rangeArray color:colorArray leftButton:@"取消" rightButton:@"确定" success:^{
[weakSelf setcardNetWorking];
} cancel:^{
} orDelete:^{
}];
[[self viewController] presentViewController:vc animated:YES completion:^{
}];
按钮文字变化的
__weak typeof(self) weakSelf = self;
SJZAlertViewController *vc = [[SJZAlertViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;
[vc alerWithButtonConfigurationTitle:@"结束直播" message:@"您正在直播中,确定结束直播并退出吗?" style:SJZAlertControllerStyleDelete withRange:@[] color:@[] leftButton:@"结束直播" rightButton:@"最小化" success:^{
} cancel:^{
[weakSelf closeRoom];
} orDelete:^{
}];
[self presentViewController:vc animated:YES completion:^{
}];
其他 看下面 .h .m 文件
后续再加功能
.h 文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, SJZAlertControllerStyle) {
/** 双按钮 */
SJZAlertControllerStyleAlert,
/** 底部弹框 */
SJZAlertControllerStyleActionSheet,
/** 单按钮 */
SJZAlertControllerStyleOnly,
/** 双按钮右上侧带删除 */
SJZAlertControllerStyleDelete,
/** 单按钮 右上带删除*/
SJZAlertControllerStyleOnlyORDelete,
};
@interface SJZAlertViewController : UIViewController
/** 可配置弹框 (目前单按钮 只传右按钮文字)
title 标题
message 内容
style 类型
rangeArray 内容富文本 range 他有几个 colorArray 就有几个 后期优化
leftStr 左按钮文字 (单按钮不传)
cancel 左按钮点击事件
rightStr 右按钮文字
success 右点击事件
deletea 删除按钮点击事件
*/
- (void)alerWithButtonConfigurationTitle:(NSString *)title message:(NSString *)message style:(SJZAlertControllerStyle)style withRange:(NSMutableArray *)rangeArray color:(NSMutableArray *)colorArray leftButton:(NSString *)leftStr rightButton:(NSString *)rightStr success:(void (^)(void))success cancel:(void(^)(void))cancel orDelete:(void(^)(void))deletea;
- (void)alerSheetTitle:(NSArray *)titleArray style:(SJZAlertControllerStyle)style dataBlock:(void(^)(NSString *titleStr))succes;
@end
.m 文件
#import "SJZAlertViewController.h"
@interface SJZAlertViewController ()
@property (nonatomic ,strong) UIView *backView;
@property (nonatomic ,strong) UIButton *deleteButton;
@property (nonatomic ,strong) UILabel *titleLabel;
@property (nonatomic ,strong) UILabel *contentLabel;
@property (nonatomic ,strong) UIButton *cancelButton;
@property (nonatomic ,strong) UIButton *confirmButton;
@property (nonatomic ,copy) void (^confirmBlock)(void);
@property (nonatomic ,copy) void (^cancelBlock)(void);
@property (nonatomic ,copy) void (^deleteBlock)(void);
@property (nonatomic ,copy) void (^sheetBlock)(NSString *titleStr);
@end
@implementation SJZAlertViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
}
-(void)alerWithButtonConfigurationTitle:(NSString *)title message:(NSString *)message style:(SJZAlertControllerStyle)style withRange:(NSMutableArray *)rangeArray color:(NSMutableArray *)colorArray leftButton:(NSString *)leftStr rightButton:(NSString *)rightStr success:(void (^)(void))success cancel:(void (^)(void))cancel orDelete:(void (^)(void))deletea {
CGSize size = [SJZTools addwithNstring:message orTextSize:15 andTypeface:@"" isbold:NO maxfloat:KWidth-120 fontweight:UIFontWeightRegular];
float height = size.height> 40 ? size.height+10 : 40;
[self.view addSubview:self.backView];
[_backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(35);
make.right.mas_equalTo(-35);
make.centerY.equalTo(self.view.mas_centerY).offset(0);
make.height.mas_equalTo(130+height);
}];
_backView.layer.cornerRadius = 20;
_backView.layer.masksToBounds = YES;
[self.backView addSubview:self.titleLabel];
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(45);
make.right.mas_offset(-45);
make.top.mas_equalTo(24);
make.height.mas_equalTo(20);
}];
[self.backView addSubview:self.deleteButton];
[_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_offset(-10);
make.top.mas_equalTo(10);
make.height.width.mas_offset(26);
}];
[self.backView addSubview:self.contentLabel];
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(24);
make.right.mas_equalTo(-24);
make.top.equalTo(_titleLabel.mas_bottom).offset(15);
make.height.mas_equalTo(height);
}];
[self.backView addSubview:self.cancelButton];
[_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(24);
make.bottom.mas_equalTo(-20);
make.width.mas_equalTo((KWidth-120)/2-7.5);
make.height.mas_equalTo(42);
}];
[self.backView addSubview:self.confirmButton];
if (style == SJZAlertControllerStyleAlert) {
_deleteButton.hidden = YES;
_cancelButton.hidden = _confirmButton.hidden = NO;
self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);
} else if (style == SJZAlertControllerStyleOnly) {
_cancelButton.hidden = _deleteButton.hidden = YES;
_confirmButton.hidden = NO;
self.confirmButton.frame = CGRectMake(24, height+130-42-20, KWidth-120, 42);
} else if (style == SJZAlertControllerStyleDelete){
_deleteButton.hidden = _cancelButton.hidden = _confirmButton.hidden = NO;
self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);
} else if (style == SJZAlertControllerStyleOnlyORDelete) {
_cancelButton.hidden = YES;
_confirmButton.hidden = _deleteButton.hidden = NO;
self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);
}
if (height > 40) {
_contentLabel.textAlignment = NSTextAlignmentLeft;
} else {
_contentLabel.textAlignment = NSTextAlignmentCenter;
}
_titleLabel.text = title;
if (rangeArray.count != 0) {
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:message];
for (int i = 0; i < rangeArray.count; i ++) {
NSValue *value = rangeArray[i];
[attStr addAttribute:NSForegroundColorAttributeName value:colorArray[i] range:value.rangeValue];
}
_contentLabel.attributedText = attStr;
} else {
_contentLabel.text = message;
}
[_cancelButton setTitle:leftStr forState:UIControlStateNormal];
[_confirmButton setTitle:rightStr forState:UIControlStateNormal];
self.confirmBlock = ^{
if(success) {
success();
}
};
self.cancelBlock = ^{
if (cancel) {
cancel();
}
};
self.deleteBlock = ^{
if (deletea) {
deletea();
}
};
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = _confirmButton.bounds;
// 设置渐变颜色数组
gradientLayer.colors = @[(__bridge id)RGB(45, 217, 201).CGColor,(__bridge id)RGB(153, 247, 186).CGColor];
// 渐变颜色的区间分布
gradientLayer.locations = @[@0,@0.8];
// 起始位置
gradientLayer.startPoint = CGPointMake(0, 0);
// 结束位置
gradientLayer.endPoint = CGPointMake(1, 0);
gradientLayer.cornerRadius = 21;
[_confirmButton.layer addSublayer:gradientLayer];
_confirmButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightHeavy];
}
- (void)alerSheetTitle:(NSArray *)titleArray style:(SJZAlertControllerStyle)style dataBlock:(void (^)(NSString * _Nonnull))succes {
self.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;
[self.view addSubview:self.backView];
self.backView.frame = CGRectMake(0, KHeight-titleArray.count*50-kTabbarSafeBottomMargin, KWidth, titleArray.count*50+kTabbarSafeBottomMargin);
UIRectCorner corner = UIRectCornerTopRight | UIRectCornerTopLeft;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_backView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _backView.bounds;
maskLayer.path = path.CGPath;
_backView.layer.masksToBounds = YES;
_backView.layer.mask = maskLayer;
for (int i = 0; i < titleArray.count; i ++) {
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 50*i, KWidth, 50)];
[titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
[titleButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
titleButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
[titleButton addTarget:self action:@selector(ClickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
[_backView addSubview:titleButton];
}
self.sheetBlock = ^(NSString *titleStr) {
if (succes) {
succes(titleStr);
}
};
}
- (void)ClickTitleButton:(UIButton *)btn {
if (self.sheetBlock) {
self.sheetBlock(btn.titleLabel.text);
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)selectClickConfirm:(UIButton *)btn {
if (self.confirmBlock) {
self.confirmBlock();
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)selectClickCancel:(UIButton *)btn {
if (self.cancelBlock) {
self.cancelBlock();
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)selectClickDelete:(UIButton *)btn {
if (self.deleteBlock) {
self.deleteBlock();
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
#pragma mark ----- UI
- (UIView *)backView {
if (!_backView) {
_backView = [[UIView alloc] init];
_backView.backgroundColor = [UIColor whiteColor];
}
return _backView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = BLACKCOLOR;
_titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UIButton *)deleteButton {
if (!_deleteButton) {
_deleteButton = [[UIButton alloc] init];
[_deleteButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
[_deleteButton addTarget:self action:@selector(selectClickDelete:) forControlEvents:UIControlEventTouchUpInside];
}
return _deleteButton;
}
- (UILabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[UILabel alloc] init];
_contentLabel.textColor = BLACKCOLOR;
_contentLabel.textAlignment = NSTextAlignmentCenter;
_contentLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
_contentLabel.numberOfLines = 0;
}
return _contentLabel;
}
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [[UIButton alloc] init];
[_cancelButton setBackgroundColor:RGBA(244, 247, 247, 1)];
_cancelButton.layer.masksToBounds = YES;
_cancelButton.layer.cornerRadius = 21;
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_cancelButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightHeavy];
[_cancelButton addTarget:self action:@selector(selectClickCancel:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIButton *)confirmButton {
if (!_confirmButton) {
_confirmButton = [[UIButton alloc] init];
_confirmButton.layer.masksToBounds = YES;
_confirmButton.layer.cornerRadius = 21;
[_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[_confirmButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
[_confirmButton addTarget:self action:@selector(selectClickConfirm:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
标签:confirmButton,mas,自定义,make,弹框,backView,void,self From: https://www.cnblogs.com/xiaodeng90/p/17536233.html