自己写了一个AlertView

最近发现公司项目改版后, 提示框又发生了变化。
之前因为UI觉得iOS系统自带的提示框太丑了, 所以在页面里面自己定义了提示框, 现在又在提示框里面增加了图片,觉得心塞塞, 于是决定自己写一个提示框, 以便在项目里面使用。
因为是为了满足自己的项目需求, 所以是按照需求写的。
大致的需求有几点:

1.提示框头部可能需要有图片
2.提示框里面的提示信息有的需要居中, 有的需要偏左
3.可能需要一条提示能够进行点击
4.可能只需要一个确定按钮
5.不需要按钮时, 能点击提示框
6.黑色透明背景能够点击

为了可修改性, 我把全部的属性方法都放在.h里面, 以便进行调用,就不考虑安全性啦

注意: 
1.里面的UIView+frame文件,是为了能够是的UIView是够直接使用它的width,height,x, y�等属性, 网上有很多, 就不贴出来了

2.因为是根据控件宽度, 字号来自适应高度的, 所以如果需要改变x, width的代码需要下载改变字号的代码之前

直接把代码贴上来:
DIYAlertView.h

#import <UIKit/UIKit.h>

@class DIYAlertView;
#pragma mark - 代理
@protocol DIYAlertViewDelegate <NSObject>

@optional
// button的点击方法
-(void)clickDIYAlertView:(DIYAlertView *)DIYAlertview withButtonIndex:(NSInteger)index;

// 可点击链接的点击方法
-(void)tapClickLabelWithView:(UIView *)view;

// 提示框的点击方法(一般是点击让提示框消失)
-(void)tapViewWithView:(DIYAlertView *)diyAlertview;


// 点击黑色背景的方法
-(void)tapSelfWithView:(DIYAlertView *)diyAlertview;
@end


@interface DIYAlertView : UIView

@property(nonatomic, assign)id<DIYAlertViewDelegate> delegate;


 /**
 *  self 是弹窗的黑色透明的View
 */
// self 的透明度
@property(nonatomic, assign)CGFloat Opacity;

/**
 *  弹窗的白色(默认)视图, 在其上面再加其他的界面
 */
@property(nonatomic, strong)UIView *whiteView;
// 弹窗的frame
@property(nonatomic, assign)CGRect whiteViewFrame;
// 弹窗的背景色
@property(nonatomic, strong)UIColor *whiteViewColor;


/**
 *  提示框的图片视图
 */
@property(nonatomic, strong)UIImageView *headerImgView;
// 提示框图片视图的image
@property(nonatomic, strong)UIImage *img;
// 提示框图片视图的frame
@property(nonatomic, assign)CGRect ImgViewFrame;


/**
 *  提示框的标题(默认居中)
 */
@property(nonatomic, strong)UILabel *titleLabel;
// 提示框标题的frame
@property(nonatomic, assign)CGRect titleFrame;
// 提示框标题的font
@property(nonatomic, strong)UIFont *titleFont;
// 提示框标题的color
@property(nonatomic, strong)UIColor *titleColor;
// 提示框的标题显示模式(居中, 偏左, 偏右)
@property(nonatomic, assign)NSTextAlignment titleAlignment;


/**
 *  提示框的副标题(默认居中)
*/
@property(nonatomic, strong)UILabel *assistantTitleLabel;
// 提示框的副标题的frame
@property(nonatomic, assign)CGRect assistantTitlFrame;
// 提示框的副标题的font
@property(nonatomic, strong)UIFont *assistantTitleFont;
// 提示框的副标题的color
@property(nonatomic, strong)UIColor *assistantTitleColor;
// 提示框的副标题显示模式(居中, 偏左, 偏右)
@property(nonatomic, assign)NSTextAlignment     assistantTitleAlignment;


/**
 *  提示框的信息(默认居中)
 */
@property(nonatomic, strong)UILabel *msgLabel;
// 提示框的信息的frame
@property(nonatomic, assign)CGRect msgFrame;
// 提示框的信息的font
@property(nonatomic, strong)UIFont *msgFont;
// 提示框的信息的color
@property(nonatomic, strong)UIColor *msgColor;
// 提示框的信息的显示模式(居中, 偏左, 偏右)
@property(nonatomic, assign)NSTextAlignment msgAlignment;


/**
 *  提示框的可点击链接的提示信息(默认居中)
 */
@property(nonatomic, strong)UILabel *clickLabel;
// 提示框的可点击链接的frame
@property(nonatomic, assign)CGRect clickFrame;
// 提示框的可点击链接的font
@property(nonatomic, strong)UIFont *clickFont;
// 提示框的可点击链接的color
@property(nonatomic, strong)UIColor *clickColor;
// 提示框的信息的显示模式
@property(nonatomic, assign)NSTextAlignment clickAlignment;


/**
 *  横向的分割线
 */
@property(nonatomic, strong)UILabel *Hline;
// 横向分割线的高度
@property(nonatomic, assign)CGFloat HlineH;
// 横向分割线的y
@property(nonatomic, assign)CGFloat HlineY;
// 横向分割线的color
 @property(nonatomic, strong)UIColor *HlineColor;


/**
 *  竖向的分割线
 */
@property(nonatomic, strong)UILabel *Vline;
// 竖向分割线的宽度
@property(nonatomic, assign)CGFloat VlineW;
// 竖向分割线的高度
@property(nonatomic, assign)CGFloat VlineH;
// 竖向分割线的颜色
@property(nonatomic, strong)UIColor *VlineColor;


/**
 *  取消按钮
 */
 @property(nonatomic, strong)UIButton *cancelBtn;
// 取消按钮的标题颜色
@property(nonatomic, strong)UIColor *cancelBtnColor;
// 取消按钮的font
@property(nonatomic, strong)UIFont *cancelBtnFont;


/**
 *  确定按钮
 */
@property(nonatomic, strong)UIButton *confirmBtn;
 // 确定按钮的标题颜色
@property(nonatomic, strong)UIColor *confirmBtnColor;
// 确定按钮的font
@property(nonatomic, strong)UIFont *confirmBtnFont;

// 全局控件的左边间距
@property(nonatomic, assign)CGFloat LeftDis;

#pragma mark - init弹窗
-(id)initWithImg:(UIImage *)img title:(NSString *)title assistantTitle:(NSString *)assistantTitle msg:(NSString *)msg click:(NSString *)click cancelButton:(NSString *)cancelButton  confirmButton:(NSString *)confirmButton;


#pragma mark - 显示
-(void)show;

#pragma mark - 隐藏
-(void)hide;


@end

DIYAlertView.m
#import "DIYAlertView.h"
#import "UIView+frame.h"

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define FitValue(value) ((value)/(320.0f*2)*[UIScreen mainScreen].bounds.size.width)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define SELFALPHA  0.2
#define LEFTX  FitValue(40)
#define IMGSIZE  FitValue(60)
#define DISTANCE  FitValue(20)
#define HEADERDIS  FitValue(30)
#define VIEWW  FitValue(420)
#define BUTTONH  FitValue(88)
#define TEXTFONT  FitValue(30)
#define BUTTONFONT  FitValue(30)
#define TITLECOLOR  UIColorFromRGB(0x4D4D4D)
#define MSGCOLOR  UIColorFromRGB(0x4D4D4D)
#define CLICKCOLOR  UIColorFromRGB(0x4D4D4D)
#define CANCELCOLOR  UIColorFromRGB(0xF00000)
#define CONFIRMCOLOR  UIColorFromRGB(0x1DBAFD)
#define LINECOLOR  UIColorFromRGB(0xEDEDED)
@implementation DIYAlertView
{
 UIImage *Img;
NSString *Title;
NSString *AssistantTitle;
NSString *Msg;
NSString *Click;
NSString *CancelButton;
NSString *ConfirmButton;

NSInteger cancelIndex;
NSInteger confirmIndex;
DIYAlertView *alertView;

CGFloat yyy;
}

-(id)initWithImg:(UIImage *)img title:(NSString *)title assistantTitle:(NSString *)assistantTitle msg:(NSString *)msg click:(NSString *)click cancelButton:(NSString *)cancelButton confirmButton:(NSString *)confirmButton
{
self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
if (self)
{
    Img = img;
    Title = title;
    AssistantTitle = assistantTitle;
    Msg = msg;
    Click = click;
    CancelButton = cancelButton;
    ConfirmButton = confirmButton;
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:SELFALPHA];
    
    self.whiteView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - VIEWW)/2, (kScreenHeight - FitValue(500))/2, VIEWW, FitValue(100))];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.layer.cornerRadius = FitValue(10);
    
    
    // 图片
    self.headerImgView = [UIImageView new];
    if (Img != nil)
    {
        self.headerImgView.frame = CGRectMake((self.whiteView.width - FitValue(60))/2, HEADERDIS, IMGSIZE, IMGSIZE);
        self.headerImgView.image = Img;
        [self.whiteView addSubview:_headerImgView];
    }
    else
    {
        self.headerImgView = nil;
    }
    
    NSLog(@"Img--%f", CGRectGetMaxY(self.headerImgView.frame));
    
    // �标题
    self.titleLabel = [UILabel new];
    if (Title != nil)
    {
        self.titleLabel.frame = CGRectMake(LEFTX, CGRectGetMaxY(self.headerImgView.frame) + HEADERDIS, self.whiteView.width - 2*LEFTX, [self heightForString:Title width:(self.whiteView.width - 2*LEFTX) font:[UIFont systemFontOfSize:TEXTFONT]]);
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont systemFontOfSize:TEXTFONT];
        self.titleLabel.textColor = TITLECOLOR;
        self.titleLabel.text = Title;
        [self.whiteView addSubview:_titleLabel];
        yyy = CGRectGetMaxY(self.titleLabel.frame) + DISTANCE;
    }
    else
    {
        self.titleLabel = nil;
        yyy = CGRectGetMaxY(self.headerImgView.frame) + HEADERDIS;
    }
    NSLog(@"Title--%f", yyy);

    // 副标题
    self.assistantTitleLabel = [UILabel new];
    if (AssistantTitle != nil)
    {
        self.assistantTitleLabel.frame = CGRectMake(LEFTX, yyy, self.whiteView.width - 2*LEFTX, [self heightForString:AssistantTitle width:(self.whiteView.width - 2*LEFTX) font:[UIFont systemFontOfSize:TEXTFONT]]);
        self.assistantTitleLabel.textAlignment = NSTextAlignmentCenter;
        self.assistantTitleLabel.numberOfLines = 0;
        self.assistantTitleLabel.font = [UIFont systemFontOfSize:TEXTFONT];
        self.assistantTitleLabel.textColor = TITLECOLOR;
        self.assistantTitleLabel.text = AssistantTitle;
        [self.whiteView addSubview:_assistantTitleLabel];
        yyy = CGRectGetMaxY(self.assistantTitleLabel.frame) + DISTANCE;
    }
    else
    {
        self.assistantTitleLabel = nil;
    }
    
    // 提示信息
    self.msgLabel = [UILabel new];
    if (Msg != nil)
    {
        self.msgLabel.frame = CGRectMake(LEFTX, yyy, self.whiteView.width - 2*LEFTX, [self heightForString:Msg width:(self.whiteView.width - 2*LEFTX) font:[UIFont systemFontOfSize:TEXTFONT]]);
        self.msgLabel.textAlignment = NSTextAlignmentCenter;
        self.msgLabel.numberOfLines = 0;
        self.msgLabel.font = [UIFont systemFontOfSize:TEXTFONT];
        self.msgLabel.textColor = MSGCOLOR;
        self.msgLabel.text = Msg;
        [self.whiteView addSubview:_msgLabel];
        yyy = CGRectGetMaxY(self.msgLabel.frame) + DISTANCE;
    }
    else
    {
        self.msgLabel = nil;
    }
    
    // 可点击信息
    self.clickLabel = [UILabel new];
    if (Click != nil)
    {
        self.clickLabel.frame = CGRectMake(LEFTX, yyy, self.whiteView.width - 2*LEFTX, [self heightForString:Click width:(self.whiteView.width - 2*LEFTX) font:[UIFont systemFontOfSize:TEXTFONT]]);
        self.clickLabel.textAlignment = NSTextAlignmentCenter;
        self.clickLabel.numberOfLines = 0;
        self.clickLabel.font = [UIFont systemFontOfSize:TEXTFONT];
        self.clickLabel.textColor = CLICKCOLOR;
        self.clickLabel.text = Click;
        self.clickLabel.userInteractionEnabled = YES;
        [self.whiteView addSubview:_clickLabel];
        yyy = CGRectGetMaxY(self.clickLabel.frame) + HEADERDIS;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self.clickLabel addGestureRecognizer:tap];
    }
    else
    {
        self.clickLabel = nil;
    }
    
    
    // 横向分割线
    self.Hline = [UILabel new];
    self.Vline = [UILabel new];
    self.cancelBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.confirmBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    CGFloat xxx = 0;
    if (CancelButton != nil && ConfirmButton != nil)
    {
        self.Hline = [[UILabel alloc]initWithFrame:CGRectMake(0, yyy, self.whiteView.width, FitValue(2))];
        self.Hline.backgroundColor = LINECOLOR;
        [self.whiteView addSubview:_Hline];
        
        yyy = CGRectGetMaxY(self.Hline.frame);
        
        self.Vline.frame = CGRectMake(self.whiteView.width/2, yyy, FitValue(2), BUTTONH);
        self.Vline.backgroundColor = LINECOLOR;
        [self.whiteView addSubview:_Vline];
        
        self.cancelBtn.frame = CGRectMake(0, yyy, self.whiteView.width - CGRectGetMidX(self.Vline.frame), self.Vline.height);
        [self.cancelBtn setTitle:CancelButton forState:(UIControlStateNormal)];
        [self.cancelBtn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.cancelBtn setTitleColor:CANCELCOLOR forState:(UIControlStateNormal)];
        self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:BUTTONFONT];
        [self.whiteView addSubview:_cancelBtn];
        
        xxx = CGRectGetMaxX(self.Vline.frame);
        
        self.confirmBtn.frame = CGRectMake(xxx, yyy, self.whiteView.width - xxx, BUTTONH);
        [self.confirmBtn setTitle:ConfirmButton forState:(UIControlStateNormal)];
        [self.confirmBtn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.confirmBtn setTitleColor:CONFIRMCOLOR forState:(UIControlStateNormal)];
        self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:BUTTONFONT];
        [self.whiteView addSubview:_confirmBtn];
        yyy = CGRectGetMaxY(self.confirmBtn.frame);
        
        self.cancelBtn.tag = 0;
        self.confirmBtn.tag = 1;
    }
    else if (CancelButton == nil && ConfirmButton != nil)
    {
        self.Vline = nil;
        self.cancelBtn = nil;
        
        self.Hline = [[UILabel alloc]initWithFrame:CGRectMake(0, yyy, self.whiteView.width, FitValue(2))];
        self.Hline.backgroundColor = LINECOLOR;
        [self.whiteView addSubview:_Hline];
        
        yyy = CGRectGetMaxY(self.Hline.frame);
        
        self.confirmBtn.tag = 0;
        
        self.confirmBtn.frame = CGRectMake(xxx, yyy, self.whiteView.width - xxx, BUTTONH);
        [self.confirmBtn setTitle:ConfirmButton forState:(UIControlStateNormal)];
        [self.confirmBtn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.confirmBtn setTitleColor:CONFIRMCOLOR forState:(UIControlStateNormal)];
        self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:BUTTONFONT];
        [self.whiteView addSubview:_confirmBtn];
        yyy = CGRectGetMaxY(self.confirmBtn.frame);

    }
    else
    {
        self.Hline = nil;
        self.Vline = nil;
        self.cancelBtn = nil;
        self.confirmBtn = nil;
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
        [self.whiteView addGestureRecognizer:tap1];
        
    }
    
    self.whiteView.height = yyy;
    
    [self addSubview:_whiteView];
    
    
    
    UITapGestureRecognizer *tapSelf = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapSelf:)];
    [self addGestureRecognizer:tapSelf];
}

alertView = self;
return self;
}


#pragma mark - �self
// self 的透明度
-(void)setOpacity:(CGFloat)Opacity
{
_Opacity = Opacity;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:Opacity];
}


#pragma mark - 白色的弹窗
-(void)setWhiteView:(UIView *)whiteView
{
_whiteView = whiteView;
}

//弹窗的frame
-(void)setWhiteViewFrame:(CGRect)whiteViewFrame
{
_whiteViewFrame = whiteViewFrame;
self.whiteView.frame = whiteViewFrame;

self.headerImgView.x = (self.whiteView.width - self.headerImgView.width)/2;
self.titleLabel.width = self.whiteView.width - 2*LEFTX;
self.assistantTitleLabel.width = self.whiteView.width - 2*LEFTX;;
self.msgLabel.width = self.whiteView.width - 2*LEFTX;;
self.clickLabel.width = self.whiteView.width - 2*LEFTX;
self.Hline.width = self.whiteView.width;

CGFloat xx = 0;
if (CancelButton!= nil)
{
    self.Vline.x = self.whiteView.width/2;
    self.cancelBtn.x = 0;
    self.cancelBtn.width = self.whiteView.width - CGRectGetMidX(self.Vline.frame);
    xx = CGRectGetMaxX(self.Vline.frame);
}
self.confirmBtn.x = xx;
self.confirmBtn.width = self.whiteView.width - xx;
}

// 弹窗的背景色
-(void)setWhiteViewColor:(UIColor *)whiteViewColor
{
_whiteViewColor = whiteViewColor;
self.whiteView.backgroundColor = whiteViewColor;
}



#pragma mark - 提示框的图片
-(void)setHeaderImgView:(UIImageView *)headerImgView
{
_headerImgView = headerImgView;
}

// 提示框图片
-(void)setImg:(UIImage *)img
{
_img = img;
self.headerImgView.image = img;
}

// 提示框图片视图的frame
-(void)setImgViewFrame:(CGRect)ImgViewFrame
{
_ImgViewFrame = ImgViewFrame;
self.headerImgView.frame = ImgViewFrame;
[self resetItemsFrame];
}



 #pragma mark - 提示框的标题
-(void)setTitleLabel:(UILabel *)titleLabel
{
_titleLabel = titleLabel;
}

// 提示框标题的frame
 -(void)setTitleFrame:(CGRect)titleFrame
{
_titleFrame = titleFrame;
self.titleLabel.frame = titleFrame;
[self resetItemsFrame];
}

// 提示框标题的font
-(void)setTitleFont:(UIFont *)titleFont
{
_titleFont = titleFont;
self.titleLabel.font = titleFont;
self.titleLabel.height = [self heightForString:Title width:self.titleLabel.width font:titleFont];
[self resetItemsFrame];
}

// 提示框标题的color
-(void)setTitleColor:(UIColor *)titleColor
{
_titleColor = titleColor;
self.titleLabel.textColor = titleColor;
}

// 提示框标题的显示模式
-(void)setTitleAlignment:(NSTextAlignment)titleAlignment
{
_titleAlignment = titleAlignment;
self.titleLabel.textAlignment = titleAlignment;
}


#pragma mark - 提示框的副标题
-(void)setAssistantTitleLabel:(UILabel *)assistantTitleLabel
{
_assistantTitleLabel = assistantTitleLabel;
}

// 提示框副标题的frame
-(void)setAssistantTitlFrame:(CGRect)assistantTitlFrame
{
_assistantTitlFrame = assistantTitlFrame;
self.assistantTitleLabel.frame = assistantTitlFrame;
[self resetItemsFrame];
}

// 提示框副标题的font
-(void)setAssistantTitleFont:(UIFont *)assistantTitleFont
{
_assistantTitleFont = assistantTitleFont;
self.assistantTitleLabel.font = assistantTitleFont;
self.assistantTitleLabel.height = [self heightForString:AssistantTitle width:self.assistantTitleLabel.width font:assistantTitleFont];
[self resetItemsFrame];
}

// 提示框副标题的color
-(void)setAssistantTitleColor:(UIColor *)assistantTitleColor
{
_assistantTitleColor = assistantTitleColor;
self.assistantTitleLabel.textColor = assistantTitleColor;
}

// 提示框副标题的显示模式
-(void)setAssistantTitleAlignment:(NSTextAlignment)assistantTitleAlignment
{
_assistantTitleAlignment = assistantTitleAlignment;
self.assistantTitleLabel.textAlignment = assistantTitleAlignment;
}


#pragma mark - 提示框的提示信息
-(void)setMsgLabel:(UILabel *)msgLabel
{
_msgLabel = msgLabel;
}

// 提示框提示信息的frame
-(void)setMsgFrame:(CGRect)msgFrame
{
_msgFrame = msgFrame;
self.msgLabel.frame = msgFrame;
[self resetItemsFrame];
}

// 提示框提示信息的font
-(void)setMsgFont:(UIFont *)msgFont
{
_msgFont = msgFont;
self.msgLabel.font = msgFont;
self.msgLabel.height = [self heightForString:Msg width:self.msgLabel.width font:msgFont];

[self resetItemsFrame];
}

// 提示框提示信息的color
-(void)setMsgColor:(UIColor *)msgColor
{
_msgColor = msgColor;
self.msgLabel.textColor = msgColor;
}

// 提示框提示信息的显示模式
-(void)setMsgAlignment:(NSTextAlignment)msgAlignment
{
_msgAlignment = msgAlignment;
self.msgLabel.textAlignment = msgAlignment;
}


#pragma mark - 提示框可点击链接
-(void)setClickLabel:(UILabel *)clickLabel
{
_clickLabel = clickLabel;
}

// 提示框可点击链接frame
-(void)setClickFrame:(CGRect)clickFrame
{
_clickFrame = clickFrame;
self.clickLabel.frame = clickFrame;
[self resetItemsFrame];
}

// 提示框可点击链接font
-(void)setClickFont:(UIFont *)clickFont
{
_clickFont = clickFont;
self.clickLabel.font = clickFont;
self.clickLabel.height = [self heightForString:Click width:self.clickLabel.width font:clickFont];
[self resetItemsFrame];
}

// 提示框可点击链接color
-(void)setClickColor:(UIColor *)clickColor
{
_clickColor = clickColor;
self.clickLabel.textColor = clickColor;
}

// 提示框可点击链接的显示模式
-(void)setClickAlignment:(NSTextAlignment)clickAlignment
{
_clickAlignment = clickAlignment;
self.clickLabel.textAlignment = clickAlignment;
}


#pragma mark - 横向分割线
-(void)setHline:(UILabel *)Hline
{
_Hline = Hline;
 }

// 横向分割线的高度
-(void)setHlineH:(CGFloat)HlineH
{
_HlineH = HlineH;
self.Hline.height = HlineH;
[self resetItemsFrame];
}

// 横向分割线的y
-(void)setHlineY:(CGFloat)HlineY
{
_HlineY = HlineY;
self.Hline.y = HlineY;
yyy = CGRectGetMaxY(self.Hline.frame);

if (CancelButton != nil)
{
    self.Vline.y = yyy;
    self.cancelBtn.y = yyy;
}
self.confirmBtn.y = yyy;

self.whiteView.height = yyy + self.confirmBtn.height;
}

// 横向分割线的color
-(void)setHlineColor:(UIColor *)HlineColor
{
_HlineColor = HlineColor;
self.Hline.backgroundColor = HlineColor;
}


#pragma mark - 竖向分割线
-(void)setVline:(UILabel *)Vline
{
_Vline = Vline;
}

// 竖向分割线宽度
-(void)setVlineW:(CGFloat)VlineW
{
_VlineW = VlineW;
self.Vline.width = VlineW;
self.Vline.x = self.whiteView.width/2 - VlineW/2;
self.cancelBtn.width = CGRectGetMidX(self.Vline.frame);
self.confirmBtn.x = CGRectGetMaxX(self.Vline.frame);
self.confirmBtn.width = self.cancelBtn.width;
}

// 竖向分割线高度
-(void)setVlineH:(CGFloat)VlineH
{
_VlineH = VlineH;
self.Vline.height = VlineH;
self.cancelBtn.height = VlineH;
self.confirmBtn.height = VlineH;
self.whiteView.height = self.Vline.y + VlineH;
}

// 竖向分割线color
-(void)setVlineColor:(UIColor *)VlineColor
 {
_VlineColor = VlineColor;
self.Vline.backgroundColor = VlineColor;
}


#pragma mark - 取消按钮
-(void)setCancelBtn:(UIButton *)cancelBtn
{
_cancelBtn = cancelBtn;
}

// 取消按钮的标题颜色
-(void)setCancelBtnColor:(UIColor *)cancelBtnColor
{
_cancelBtnColor = cancelBtnColor;
[self.cancelBtn setTitleColor:cancelBtnColor forState:(UIControlStateNormal)];
}

// 取消按钮的font
-(void)setCancelBtnFont:(UIFont *)cancelBtnFont
{
_cancelBtnFont = cancelBtnFont;
self.cancelBtn.titleLabel.font = cancelBtnFont;
}


#pragma mark - 确定按钮
-(void)setConfirmBtn:(UIButton *)confirmBtn
{
_confirmBtn = confirmBtn;
}

// 确定按钮的标题颜色
-(void)setConfirmBtnColor:(UIColor *)confirmBtnColor
{
_confirmBtnColor = confirmBtnColor;
[self.confirmBtn setTitleColor:confirmBtnColor forState:(UIControlStateNormal)];
}

// 确定按钮的font
-(void)setConfirmBtnFont:(UIFont *)confirmBtnFont
{
_confirmBtnFont = confirmBtnFont;
self.confirmBtn.titleLabel.font = confirmBtnFont;
}

#pragma mark - 全局左边间距
-(void)setLeftDis:(CGFloat)LeftDis
{
_LeftDis = LeftDis;
self.titleLabel.x = LeftDis;
self.titleLabel.width = self.whiteView.width - 2*LeftDis;
self.assistantTitleLabel.x = LeftDis;
self.assistantTitleLabel.width = self.whiteView.width - 2*LeftDis;
self.msgLabel.x = LeftDis;
self.msgLabel.width = self.whiteView.width - 2*LeftDis;
self.clickLabel.x = LeftDis;
self.clickLabel.width = self.whiteView.width - 2*LeftDis;
}

#pragma mark - 私有方法
/**
 *  重新设置控件的纵向frame
 */
-(void)resetItemsFrame
{
yyy = CGRectGetMaxY(self.headerImgView.frame) + HEADERDIS;
if (Title != nil)
{
    self.titleLabel.y = yyy;
    yyy = CGRectGetMaxY(self.titleLabel.frame) + DISTANCE;
}

if (AssistantTitle != nil)
{
    self.assistantTitleLabel.y = yyy;
    yyy = CGRectGetMaxY(self.assistantTitleLabel.frame) + DISTANCE;
}

if (Msg != nil)
{
    self.msgLabel.y = yyy;
    yyy = CGRectGetMaxY(self.msgLabel.frame) + DISTANCE;
}

if (Click != nil)
{
    self.clickLabel.y = yyy;
    yyy = CGRectGetMaxY(self.clickLabel.frame) + HEADERDIS;
}



if (CancelButton != nil && ConfirmButton != nil)
{
    self.Hline.y = yyy;
    yyy = CGRectGetMaxY(self.Hline.frame);
    self.Vline.y = yyy;
    self.cancelBtn.y = yyy;
    self.confirmBtn.y = yyy;
    yyy = CGRectGetMaxY(self.confirmBtn.frame);
}
else if (CancelButton == nil && ConfirmButton != nil)
{
    self.Hline.y = yyy;
    yyy = CGRectGetMaxY(self.Hline.frame);
    self.confirmBtn.y = yyy;
    yyy = CGRectGetMaxY(self.confirmBtn.frame);
}


self.whiteView.height = yyy;
}
#pragma mark - 计算文本高度
-(CGFloat)heightForString:(NSString *)string width:(CGFloat)width font:(UIFont *)font
{
CGRect bounds = [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return bounds.size.height;
}

#pragma mark - 提示框显示
-(void)show
{
self.layer.zPosition = INT8_MAX;
UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
[window addSubview:self];
[window bringSubviewToFront:self];
}

-(void)hide
{
[self removeFromSuperview];
}

#pragma mark - button的点击方法
-(void)click:(UIButton *)btn
{
if ([self.delegate respondsToSelector:@selector(clickDIYAlertView:withButtonIndex:)]) {
    [self.delegate clickDIYAlertView:alertView withButtonIndex:btn.tag];
}
}

#pragma mark - 可点击label的点击方法
-(void)tap:(UITapGestureRecognizer *)tap
{
if ([self.delegate respondsToSelector:@selector(tapClickLabelWithView:)])
{
    [self.delegate tapClickLabelWithView:tap.view];
}
}

#pragma mark - 没有按钮时点击提示框的方法
-(void)tapView:(UITapGestureRecognizer *)tap
{
//    if ([self.delegate respondsToSelector:@selector(tapViewWithView:)])
//    {
    [self.delegate tapViewWithView:(DIYAlertView *)tap.view.superview];
//    }
}

#pragma mark - 点击透明黑色背景的方法
-(void)tapSelf:(UITapGestureRecognizer *)tap
{
if ([self.delegate respondsToSelector:@selector(tapSelfWithView:)])
{
    [self.delegate tapSelfWithView:(DIYAlertView *)tap.view];
}
}
@end

控制器里面的代码:
ViewDidLoad

// 先定义一个button, 点击button出现提示框(如果提示框直接写在ViewDidLoad里面,可能控制器自己的View还没有显示出来, 先把提示框显示出来, 会使得提示框在控制器View的下面, 无法点击
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor grayColor];
    [button addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];

-(void)button:(UIButton *)btn
{
DIYAlertView *DIY = [[DIYAlertView alloc]initWithImg:[UIImage imageNamed:@"ic_user_centerr@2x"] title:@"快递员将收\n到短信提醒" assistantTitle:@"问题件将由快递员收回" msg:nil click:nil cancelButton:@"取消" confirmButton:@"确定"];
DIY.whiteViewFrame = CGRectMake(FitValue(20), FitValue(100), FitValue(700), FitValue(100));
DIY.ImgViewFrame = CGRectMake((DIY.whiteView.width - FitValue(100))/2, FitValue(100), FitValue(100), FitValue(100));
DIY.titleAlignment = NSTextAlignmentLeft;
DIY.titleColor = [UIColor greenColor];
DIY.assistantTitleFont = [UIFont systemFontOfSize:FitValue(60)];
DIY.delegate = self;
[DIY show];
}

-(void)clickDIYAlertView:(DIYAlertView *)DIYAlertview withButtonIndex:(NSInteger)index
{
if (index == 0)
{
    [DIYAlertview hide];
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,271评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,725评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,252评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,634评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,549评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,985评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,471评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,128评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,257评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,233评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,235评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,940评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,528评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,623评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,858评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,245评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,790评论 2 339

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,251评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,975评论 4 60
  • 一、一直以来的认知,记忆不重要 高中时期,对于题海,填鸭式教育很抵触,认为学习是以理解为基础的,只要懂了就要可以,...
    践行而生阅读 328评论 1 1
  • 文/星姐 “望子成龙,望女成凤”是天下所有父母的夙愿,做父母的,都希望自己的孩子能够一生快乐无忧,成人成才。 而教...
    大脚宝贝阅读 564评论 1 3
  • 七毛 十字路口,红灯亮了。前面停住一辆货车,上面堆满了西瓜。一个年轻人坐在西瓜上。公交车抖了抖,稳住,司机推开车窗...
    毕晓普阅读 252评论 0 1