(instancetype)initWithTitle:(NSString )title message:(NSString )message sureBtn:(NSString )sureTitle cancleBtn:(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 = [UIColor whiteColor];
self.alertView.layer.cornerRadius = 5.0;
self.alertView.frame = CGRectMake(0,AlertW,100);
self.alertView.layer.position = self.center;
if (title) {
self.titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace,2*XLSpace,AlertW-4*XLSpace,20) AndText:title andIsTitle:YES];
self.titleLbl.textAlignment = NSTextAlignmentCenter;
[self.alertView addSubview:self.titleLbl];
CGFloat titleW = self.titleLbl.bounds.size.width;
CGFloat titleH = self.titleLbl.bounds.size.height;
self.titleLbl.frame = CGRectMake((AlertW-titleW)/2,titleW,titleH);
}
if (message) {
self.msgLbl = [self GetAdaptiveLable:CGRectMake(XLSpace,CGRectGetMaxY(self.titleLbl.frame)+XLSpace,AlertW-2*XLSpace,20) AndText:message andIsTitle:NO];
self.msgLbl.textAlignment = NSTextAlignmentCenter;
[self.alertView addSubview:self.msgLbl];
CGFloat msgW = self.msgLbl.bounds.size.width;
CGFloat msgH = self.msgLbl.bounds.size.height;
self.msgLbl.frame = self.titleLbl?CGRectMake((AlertW-msgW)/2,msgW,msgH):CGRectMake((AlertW-msgW)/2,msgH);
}
self.lineView = [[UIView alloc] init];
self.lineView.frame = self.msgLbl?CGRectMake(0,CGRectGetMaxY(self.msgLbl.frame)+2XLSpace,1):CGRectMake(0,CGRectGetMaxY(self.titleLbl.frame)+2XLSpace,1);
self.lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
[self.alertView addSubview:self.lineView];
//两个按钮
if (cancleTitle && sureTitle) {
self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.cancleBtn.frame = CGRectMake(0,CGRectGetMaxY(self.lineView.frame),(AlertW-1)/2,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 cornerRadii: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 (cancleTitle && sureTitle) {
self.verLineView = [[UIView alloc] init];
self.verLineView.frame = CGRectMake(CGRectGetMaxX(self.cancleBtn.frame),1,40);
self.verLineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
[self.alertView addSubview:self.verLineView];
}
if(sureTitle && cancleTitle){
self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.sureBtn.frame = CGRectMake(CGRectGetMaxX(self.verLineView.frame),(AlertW-1)/2+1,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 whiteColor] forState:UIControlStateNormal];
self.sureBtn.tag = 2;
[self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii: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,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 | UIRectCornerBottomRight cornerRadii: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,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 | UIRectCornerBottomRight cornerRadii: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 = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
self.alertView.frame = CGRectMake(0,alertHeight);
self.alertView.layer.position = self.center;
[self addSubview:self.alertView];
}
return self;
}