自己写了一个AlertView
2016-06-27 本文已影响53人
YYYLynn
最近发现公司项目改版后, 提示框又发生了变化。
之前因为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];
}
}