原本是采用的网上的一个第三方,然后拿来自己修改了下。原版请见:http://www.jb51.net/article/97468.htm
UIAlertView 自定义可行性不太好,所以就用UIView来代替了。
效果图:
上代码
.h 文件里面:
#import <UIKit/UIKit.h>
typedef void(^AlertResult)(NSInteger index);
@interface AlertView : UIView
@property (nonatomic,copy) AlertResult resultIndex;
- (instancetype)initWithimage:(UIImage *)image message:(NSString*)message sureBtn:(NSString *)sureTitlecancleBtn:(NSString *)cancleTitle;
- (void)showAlertView;
@end
.m文件里面:
//alertView 宽
#define AlertW 200
//各个栏目之间的距离
#define XLSpace 10.0
@interface AlertView()
//弹窗
@property (nonatomic, strong) UIView *alertView;
//图片
@property (nonatomic, strong) UIImageView *imageView;
//内容
@property (nonatomic, strong) UILabel *msgLbl;
//确认按钮
@property (nonatomic, strong) UIButton *sureBtn;
//取消按钮
@property (nonatomic, strong) UIButton *cancleBtn;
//第一条横线
@property (nonatomic, strong) UIView *lineView;
//第二条横线
@property (nonatomic, strong) UIView *sLineView;
创建:
- (instancetype)initWithimage:(UIImage *)image message:(NSString*)message sureBtn:(NSString *)sureTitlecancleBtn:(NSString *)cancleTitle
{
if (self == [super init]) {
self.frame= [UIScreen mainScreen].bounds;
self.backgroundColor= [UIColor colorWithWhite:0.8 alpha:0.6];
self.alertView= [[UIView alloc]init];
self.alertView.backgroundColor = [UIColorwhiteColor];
self.alertView.layer.cornerRadius = 5.0;
self.alertView.frame = CGRectMake(0, 0, AlertW, 180);
self.alertView.layer.position = self.center;
if (image) {
self.imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0, -5, AlertW, 55)];
self.imageView.image= image;
[self.alertView addSubview:self.imageView];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(self.imageView.frame)+10, 100, 15)];
label.text = @"更新内容";
label.font = [UIFont systemFontOfSize:15];
label.textColor = UPDATE_TEXTCOLOR;
[self.alertViewaddSubview:label];
if (message) {
self.msgLbl = [self GetAdaptiveLable:CGRectMake(15, CGRectGetMaxY(label.frame)+XLSpace,AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
self.msgLbl.font = [UIFont systemFontOfSize:14];
self.msgLbl.textColor = UPDATE_CONTENTCOLOR;
[self.alertView addSubview:self.msgLbl];
CGFloat msgW = self.msgLbl.bounds.size.width;
CGFloat msgH = self.msgLbl.bounds.size.height;
self.msgLbl.frame = self.imageView?CGRectMake(15, CGRectGetMaxY(label.frame)+XLSpace, msgW, msgH):CGRectMake((AlertW-msgW)/2,2*XLSpace,msgW, msgH);
}
self.lineView= [[UIView alloc]init];
self.lineView.frame = self.msgLbl?CGRectMake(0, CGRectGetMaxY(self.msgLbl.frame)+2*XLSpace, AlertW, 1):CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+2*XLSpace, AlertW, 1);
self.lineView.backgroundColor = [UIColorcolorWithWhite:0.8 alpha:0.6];
[self.alertViewaddSubview:self.lineView];
//两个按钮
if (cancleTitle && sureTitle){
self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.cancleBtn.frame= CGRectMake(0,CGRectGetMaxY(self.lineView.frame),AlertW, 40);
[self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
[self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
[self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
[self.cancleBtn setTitleColor:NOTUPDATECOLOR forState:UIControlStateNormal];
self.cancleBtn.tag =1;
[self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath =[UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeftcornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
maskLayer.frame = self.cancleBtn.bounds;
maskLayer.path =maskPath.CGPath;
self.cancleBtn.layer.mask = maskLayer;
[self.alertView addSubview:self.cancleBtn];
}
self.sLineView= [[UIView alloc]init];
self.sLineView.frame = CGRectMake(0, CGRectGetMaxY(self.cancleBtn.frame), AlertW,1);
self.sLineView.backgroundColor = [UIColorcolorWithWhite:0.8 alpha:0.6];
[self.alertViewaddSubview:self.sLineView];
if(sureTitle && cancleTitle){
self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.sureBtn.frame =CGRectMake(0,CGRectGetMaxY(self.cancleBtn.frame)+1, AlertW, 40);
[self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
[self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
[self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
[self.sureBtn setTitleColor:UPDATECOLOR forState:UIControlStateNormal];
self.sureBtn.tag = 2;
[self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath =[UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomRightcornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
maskLayer.frame = self.sureBtn.bounds;
maskLayer.path =maskPath.CGPath;
self.sureBtn.layer.mask = maskLayer;
[self.alertView addSubview:self.sureBtn];
}
//只有取消按钮
if (cancleTitle &&!sureTitle) {
self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.cancleBtn.frame= CGRectMake(0,CGRectGetMaxY(self.lineView.frame),AlertW, 40);
[self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
[self.cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
[self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
//[self.cancleBtn setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
self.cancleBtn.tag =1;
[self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath =[UIBezierPath bezierPathWithRoundedRect:self.cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRightcornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
maskLayer.frame = self.cancleBtn.bounds;
maskLayer.path =maskPath.CGPath;
self.cancleBtn.layer.mask = maskLayer;
[self.alertView addSubview:self.cancleBtn];
}
//只有确定按钮
if(sureTitle &&!cancleTitle){
self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.sureBtn.frame =CGRectMake(0,CGRectGetMaxY(self.lineView.frame),AlertW, 40);
[self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
[self.sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
[self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
//[self.sureBtn setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
self.sureBtn.tag = 2;
[self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath =[UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRightcornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
maskLayer.frame = self.sureBtn.bounds;
maskLayer.path =maskPath.CGPath;
self.sureBtn.layer.mask = maskLayer;
[self.alertView addSubview:self.sureBtn];
}
//计算高度
CGFloat alertHeight = CGRectGetMaxY(self.sureBtn.frame);
self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
self.alertView.layer.position = self.center;
[self addSubview:self.alertView];
}
returnself;
}
#pragma mark - 弹出
- (void)showAlertView
{
UIWindow *rootWindow = [UIApplicationsharedApplication].keyWindow;
[rootWindow addSubview:self];
[self creatShowAnimation];
}
弹出显示后,其他区域的背景为半透明状态
- (void)creatShowAnimation
{
self.backgroundColor= [UIColor colorWithRed:70.0 / 255.0 green:70.0 / 255.0 blue:70.0 / 255.0 alpha:0.5] ;
self.alertView.layer.position = self.center;
// self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
[UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOLfinished) {
}];
}
#pragma mark - 回调 -设置只有2 -- > 确定才回调
- (void)buttonEvent:(UIButton*)sender
{
if (sender.tag== 2) {
if (self.resultIndex) {
self.resultIndex(sender.tag);
}
}
[self removeFromSuperview];
}
-(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString*)contentStr andIsTitle:(BOOL)isTitle
{
UILabel *contentLbl = [[UILabelalloc] initWithFrame:rect];
contentLbl.numberOfLines = 0;
contentLbl.text = contentStr;
contentLbl.textAlignment = NSTextAlignmentCenter;
if (isTitle) {
contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
}else{
contentLbl.font = [UIFont systemFontOfSize:14.0];
}
NSMutableAttributedString *mAttrStr = [[NSMutableAttributedStringalloc] initWithString:contentStr];
NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStylealloc] init];
mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
[mParaStyle setLineSpacing:3.0];
[mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStrlength])];
[contentLbl setAttributedText:mAttrStr];
[contentLbl sizeToFit];
return contentLbl;
}
#自定义颜色
-(UIImage *)imageWithColor:(UIColor*)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
调用的时候这样写:
NSString *str1 = @"-优化页面";
AlertView *alertView = [[AlertView alloc]initWithimage:[UIImageimageNamed:@"dialog"] message:str1 sureBtn:@"马上升级" cancleBtn:@"暂不升级"];
alertView.resultIndex = ^(NSInteger index) {
NSString *iTunesString = [NSStringstringWithFormat:@"https://itunes.apple.com/app/id%@", kHarpyAppID];
NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
[[UIApplication sharedApplication] openURL:iTunesURL];
};
[alertView showAlertView];