自定义弹框

96
皮格赫特
2017.07.10 09:44* 字数 2140

//

//  ZXAlertView.h

//  TestIOS

//

//  Created by pigheart on 2017/7/7.//  Copyright © 2017年 pc. All rights reserved.

//#import

@interface ZXAlertView : UIView

//弹框文案

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *message;

@property (nonatomic, copy) NSString *leftBtnTitle;

@property (nonatomic, copy) NSString *rightBtnTitle;

/**

点击背景灰,是否支持隐藏弹框  YES支持

*/

@property (nonatomic, assign) BOOL isHiddenClickBack;

@property (nonatomic, strong) UIControl *backControl;//背景

//弹框控件

@property (nonatomic, strong) UILabel *titleLab;

@property (nonatomic, strong) UILabel *contentLab;

@property (nonatomic, strong) UIButton *leftBtn;

@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) UIView *horLineView;

@property (nonatomic, strong) UIView *verLineView;

//按钮点击

/**

点击按钮  clickTag:左按钮(一个按钮)- 0、右按钮 - 1

*/

@property (nonatomic, copy) void (^clickBlock) (NSInteger clickTag);

#pragma mark - 正常样式弹框

/**

弹框初始化

注:如果需要展示两个按钮,不管 普通按钮 还是 含富文本按钮 都需要赋值

@param title 内容

@param message 标题

@param leftBtnTitle 左边按钮标题

@param rightBtnTitle 右边按钮标题

@return ZXAlertView

*/

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle rightBtnTitle:(NSString *)rightBtnTitle;

/**

刷新弹框布局

已针对赋值情况做了布局调整

@param title 标题

@param message 内容

@param leftBtnTitle 左边按钮

@param rightBtnTitle 右边按钮

*/

- (void)reloadWithTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle rightBtnTitle:(NSString *)rightBtnTitle;

#pragma mark - 含富文本按钮弹框

/**

刷新单个按钮:富文本

*/

- (void)reloadOneAttributeBtnWithAllText:(NSString *)allText textColor:(UIColor *)textColor textFont:(UIFont *)textFont highText:(NSString *)highText highTextColor:(UIColor *)highTextColor highTextFont:(UIFont *)highTextFont;

/**

刷新两个按钮:左边富文本

*/

- (void)reloadLeftAttributeBtnWithAllText:(NSString *)allText textColor:(UIColor *)textColor textFont:(UIFont *)textFont highText:(NSString *)highText highTextColor:(UIColor *)highTextColor highTextFont:(UIFont *)highTextFont;

/**

刷新两个按钮:右边富文本

*/

- (void)reloadRightAttributeBtnWithAllText:(NSString *)allText textColor:(UIColor *)textColor textFont:(UIFont *)textFont highText:(NSString *)highText highTextColor:(UIColor *)highTextColor highTextFont:(UIFont *)highTextFont;

#pragma mark - 内容富文本

/**

内容富文本

为了统一,仅支持颜色替换

@param allText 文本全部内容

@param highTextArr 高亮文本数组

@param highColorArr 高亮文本颜色数组

*/

- (void)reloadContentAttributeWithAllText:(NSString *)allText highTextArr:(NSArray *)highTextArr highTextColorArr:(NSArray *)highColorArr;

#pragma mark - 展示

/**

展示弹框

*/

- (void)show;

/**

带有tag值的弹框,用于一个页面多个弹框情况

@param alertTag alertTag description

*/

- (void)showWithTag:(NSInteger)alertTag;

@end




//

//  ZXAlertView.m

//  TestIOS

//

//  Created by pigheart on 2017/7/7.

//  Copyright © 2017年 pc. All rights reserved.

//#import "ZXAlertView.h"

#define        kAlertViewW            kWidthBase6(275)

#define        kAlertBtnH              kHeightBase6(60)

#define        kAlertPaddingLRW        kWidthBase6(15)//左右边距

#define        kContentW              (kAlertViewW-2*kAlertPaddingLRW)

#define        kContentFont            FontSize(13)

#define        kContentLineSpaceH      kHeightBase6(7)

#define        kContentTextColor      hexColor(kCont_Black_333333)

#define        kBtnBaseTag            100

@implementation ZXAlertView

#pragma mark - 初始化

- (instancetype)init{    if ([super init]) {        //配置基本信息        [self configBaseData];        //布局        [self setupUI];    }    return self;}/** 弹框初始化 注:如果需要展示两个按钮,不管 普通按钮 还是 含富文本按钮 都需要赋值 @param title 内容 @param message 标题 @param leftBtnTitle 左边按钮标题 @param rightBtnTitle 右边按钮标题 @return ZXAlertView */- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle rightBtnTitle:(NSString *)rightBtnTitle{    self = [super init];    if (self) {        //配置基本信息        [self configBaseData];        //更新文案        [self updateTitle:title message:message leftBtnTitle:leftBtnTitle rightBtnTitle:rightBtnTitle];        //布局        [self setupUI];        //根据赋值刷新布局        [self reloadWithTitle:self.title message:self.message leftBtnTitle:self.leftBtnTitle rightBtnTitle:self.rightBtnTitle];    }    return self;}- (void)configBaseData{    self.backgroundColor = kWhiteColor;    self.layer.masksToBounds = YES;    self.layer.cornerRadius = 10;    self.frameWidth = kAlertViewW;}#pragma mark - 正常样式弹框/** 默认布局 */- (void)setupUI{    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kAlertViewW, kHeightBase6(50))];    titleLab.text = self.title;    titleLab.textAlignment = NSTextAlignmentCenter;    titleLab.font = FontBoldSize(17);    titleLab.textColor = kBlackColor;//    titleLab.backgroundColor = kOrangeColor;    [self addSubview:titleLab];    self.titleLab = titleLab;        UILabel *contentLab = [[UILabel alloc] initWithFrame:CGRectMake(kAlertPaddingLRW, titleLab.frameBottom, kContentW, 0)];    contentLab.text = self.message;    contentLab.textAlignment = NSTextAlignmentCenter;    contentLab.font = FontSize(13);    contentLab.textColor = kContentTextColor;    contentLab.numberOfLines = 0;//    contentLab.backgroundColor = kYellowColor;    [self addSubview:contentLab];    self.contentLab = contentLab;        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];    [leftBtn setFrame:CGRectMake(0, contentLab.frameBottom, kAlertViewW/2, kAlertBtnH)];    [leftBtn setTitle:self.leftBtnTitle forState:UIControlStateNormal];    [leftBtn setTitleColor:hexColor(kCont_Blue_24a0f2) forState:UIControlStateNormal];    [leftBtn setBackgroundImage:[UIImage imageWithColor:kWhiteColor] forState:UIControlStateNormal];    [leftBtn setBackgroundImage:[UIImage imageWithColor:hexColor(kCellSelected_Gray_e9ecf0)] forState:UIControlStateHighlighted];    leftBtn.titleLabel.font = FontSize(16);    leftBtn.titleLabel.numberOfLines = 0;    leftBtn.titleLabel.textAlignment = NSTextAlignmentCenter;    leftBtn.tag = kBtnBaseTag;    [leftBtn addTarget:self action:@selector(alertBtnClick:) forControlEvents:UIControlEventTouchUpInside];    [self addSubview:leftBtn];    self.leftBtn = leftBtn;        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];    [rightBtn setFrame:CGRectMake(kAlertViewW/2.0, contentLab.frameBottom, kAlertViewW/2.0, kAlertBtnH)];    [rightBtn setTitle:self.rightBtnTitle forState:UIControlStateNormal];    [rightBtn setTitleColor:hexColor(kCont_Blue_24a0f2) forState:UIControlStateNormal];    [rightBtn setBackgroundImage:[UIImage imageWithColor:kWhiteColor] forState:UIControlStateNormal];    [rightBtn setBackgroundImage:[UIImage imageWithColor:hexColor(kCellSelected_Gray_e9ecf0)] forState:UIControlStateHighlighted];    rightBtn.titleLabel.font = FontSize(16);    rightBtn.titleLabel.numberOfLines = 0;    rightBtn.titleLabel.textAlignment = NSTextAlignmentCenter;    rightBtn.tag = kBtnBaseTag+1;    [rightBtn addTarget:self action:@selector(alertBtnClick:) forControlEvents:UIControlEventTouchUpInside];    [self addSubview:rightBtn];    self.rightBtn = rightBtn;        UIView *horLineView = [[UIView alloc] initWithFrame:CGRectMake(0, contentLab.frameBottom-0.5, kAlertViewW, 0.5)];    horLineView.backgroundColor = hexColor(kLine_Gray_e5e5e5);    [self addSubview:horLineView];    self.horLineView = horLineView;        UIView *verLineView = [[UIView alloc] initWithFrame:CGRectMake(kAlertViewW/2-0.5, contentLab.frameBottom, 0.5, kAlertBtnH)];    verLineView.backgroundColor = hexColor(kLine_Gray_e5e5e5);    [self addSubview:verLineView];    self.verLineView = verLineView;}/** 刷新弹框布局 已针对赋值情况做了布局调整 @param title 标题 @param message 内容 @param leftBtnTitle 左边按钮 @param rightBtnTitle 右边按钮 */- (void)reloadWithTitle:(NSString *)title                message:(NSString *)message          leftBtnTitle:(NSString *)leftBtnTitle          rightBtnTitle:(NSString *)rightBtnTitle{    //更新文案    [self updateTitle:title message:message leftBtnTitle:leftBtnTitle rightBtnTitle:rightBtnTitle];    //标题是否为空处理    self.titleLab.hidden = !IsNotNullString(title);    self.contentLab.frameY = IsNotNullString(title) ? self.titleLab.frameBottom : kHeightBase6(17);//标题本身间距,如果没设标题就需要设置文本的顶部间距    self.titleLab.text = title;    //内容处理:因为内容高度是动态的,所以先要赋值    self.contentLab.text = message;    self.contentLab.attributedText = [Utils setTextWithStr:self.contentLab.text lineSpace:kContentLineSpaceH paragraphSpace:0 firstLineHeadIndent:0];    self.contentLab.frameHeight = [Utils textHeightWithStr:self.contentLab.text font:kContentFont width:kContentW lineSpace:kContentLineSpaceH paragraphSpace:0 firstLineHeadIndent:0].height;    self.contentLab.textAlignment = NSTextAlignmentCenter;    self.contentLab.hidden = !IsNotNullString(message);    //按钮处理    self.leftBtn.frameY = self.contentLab.frameBottom + (IsNotNullString(title) ? kHeightBase6(15) : 0);//处理文本和按钮之间的间距    self.rightBtn.frameY = self.leftBtn.frameY;    [self.leftBtn setTitle:leftBtnTitle forState:UIControlStateNormal];    [self.rightBtn setTitle:rightBtnTitle forState:UIControlStateNormal];    if (IsNotNullString(leftBtnTitle) && IsNotNullString(rightBtnTitle)) {//都不为空        self.leftBtn.hidden = NO;        self.rightBtn.hidden = NO;        self.horLineView.hidden = NO;        self.verLineView.hidden = NO;        self.leftBtn.frameWidth = kAlertViewW/2.0;        self.rightBtn.frameX = kAlertViewW/2.0;        //更新弹框高度        self.frameHeight = self.leftBtn.frameBottom;    } else if (!IsNotNullString(leftBtnTitle) && !IsNotNullString(rightBtnTitle)) {//都为空        self.isHiddenClickBack = YES;        self.leftBtn.hidden = YES;        self.rightBtn.hidden = YES;        self.horLineView.hidden = YES;        self.verLineView.hidden = YES;        //更新弹框高度        self.frameHeight = self.contentLab.frameBottom+kHeightBase6(15);    } else {//有一个为空        NSString *btnTitleStr = IsNotNullString(leftBtnTitle) ? leftBtnTitle : rightBtnTitle;        self.leftBtn.hidden = NO;        self.rightBtn.hidden = YES;        self.horLineView.hidden = NO;        self.verLineView.hidden = YES;        [self.leftBtn setTitle:btnTitleStr forState:UIControlStateNormal];        self.leftBtn.frameWidth = kAlertViewW;        //更新弹框高度        self.frameHeight = self.leftBtn.frameBottom;    }    //线条    self.horLineView.frameY = self.leftBtn.frameY;    self.verLineView.frameY = self.leftBtn.frameY;}#pragma mark - 含富文本按钮弹框/** 刷新单个按钮:富文本 */- (void)reloadOneAttributeBtnWithAllText:(NSString *)allText                              textColor:(UIColor *)textColor                                textFont:(UIFont *)textFont                                highText:(NSString *)highText                          highTextColor:(UIColor *)highTextColor                            highTextFont:(UIFont *)highTextFont{    //更新控件布局    self.leftBtnTitle = allText;    [self reloadWithTitle:self.title message:self.message leftBtnTitle:self.leftBtnTitle rightBtnTitle:@""];    [self setAttributeBtnWithBtn:self.leftBtn allText:allText textColor:textColor textFont:textFont highText:highText highTextColor:highTextColor highTextFont:highTextFont];}/** 刷新两个按钮:左边富文本 */- (void)reloadLeftAttributeBtnWithAllText:(NSString *)allText                                textColor:(UIColor *)textColor                                textFont:(UIFont *)textFont                                highText:(NSString *)highText                            highTextColor:(UIColor *)highTextColor                            highTextFont:(UIFont *)highTextFont{    [self setAttributeBtnWithBtn:self.leftBtn allText:allText textColor:textColor textFont:textFont highText:highText highTextColor:highTextColor highTextFont:highTextFont];}/** 刷新两个按钮:右边富文本 */- (void)reloadRightAttributeBtnWithAllText:(NSString *)allText                                textColor:(UIColor *)textColor                                  textFont:(UIFont *)textFont                                  highText:(NSString *)highText                            highTextColor:(UIColor *)highTextColor                              highTextFont:(UIFont *)highTextFont{    [self setAttributeBtnWithBtn:self.rightBtn allText:allText textColor:textColor textFont:textFont highText:highText highTextColor:highTextColor highTextFont:highTextFont];}/** 设置按钮富文本 @param btn 对应的阿牛 @param allText 全本文本 @param textColor 全本文本颜色 @param textFont 全本文本字体大小 @param highText 高亮文本 @param highTextColor 高亮文本颜色 @param highTextFont 高亮文本字体大小 */- (void)setAttributeBtnWithBtn:(UIButton *)btn                      allText:(NSString *)allText                    textColor:(UIColor *)textColor                      textFont:(UIFont *)textFont                      highText:(NSString *)highText                highTextColor:(UIColor *)highTextColor                  highTextFont:(UIFont *)highTextFont{    //设置富文本    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:allText];    //整体样式    [attr addAttribute:NSFontAttributeName value:textFont range:NSMakeRange(0, allText.length)];    [attr addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, allText.length)];    //高亮样式    [attr addAttribute:NSFontAttributeName value:highTextFont range:[allText rangeOfString:highText]];    [attr addAttribute:NSForegroundColorAttributeName value:highTextColor range:[allText rangeOfString:highText]];    //按钮赋值富文本,这两行都要写,或则没有效果    btn.titleLabel.attributedText = attr;    [btn setAttributedTitle:attr forState:UIControlStateNormal];}#pragma mark - 内容富文本/** 内容富文本 为了统一,仅支持颜色替换 @param allText 文本全部内容 @param highTextArr 高亮文本数组 @param highColorArr 高亮文本颜色数组 */- (void)reloadContentAttributeWithAllText:(NSString *)allText highTextArr:(NSArray *)highTextArr highTextColorArr:(NSArray *)highColorArr{    if(highTextArr.count != highColorArr.count){        return;    }    //设置富文本    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:allText];    //整体样式    [attr addAttribute:NSFontAttributeName value:kContentFont range:NSMakeRange(0, allText.length)];    [attr addAttribute:NSForegroundColorAttributeName value:kContentTextColor range:NSMakeRange(0, allText.length)];    //颜色    for (int i=0; i=kBtnBaseTag) {

if (self.clickBlock) {

self.clickBlock(btnTag-kBtnBaseTag);

}

}

}];

}

#pragma mark - 点击事件

/**

点击按钮

@param sender 按钮tag值

*/

- (void)alertBtnClick:(UIButton *)sender{

[self animatedOutWithTag:sender.tag];

}

/**

点击背景

*/

- (void)clearCtrClick{

[self animatedOutWithTag:0];//点击背景隐藏弹框,不需要任何操作

}

@end

随笔
Web note ad 1