AlertView的简单定制

github地址:https://github.com/HannahZheng/HHAlertTest
因项目所需,简单定制了一个AlertView,只支持图一和图二的样式。

AlertView的简单定制_第1张图片
图一.png

AlertView的简单定制_第2张图片
图二.png

需要依赖Masonry布局。
使用方法如下:

- (IBAction)show:(id)sender {
    
    [self.alertView show];
}



- (HHAlertView *)alertView{
    if (_alertView == nil) {
        _alertView = [[HHAlertView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:_alertView];
        _alertView.message = @"只有升级成正式店主可以提现";
        _alertView.detailLabel.textAlignment = NSTextAlignmentCenter;
        _alertView.detailLabel.font = [UIFont boldSystemFontOfSize:15];

        [_alertView addBtnWithType:HHAlertButtonTypeDefault btnTitle:@"去升级" block:^{
            //这里可以写相关的操作
        }];
        [_alertView addBtnWithType:HHAlertButtonTypeCancle btnTitle:@"再等等" block:nil];
    }
    return _alertView;
}

如果项目适配的系统最低是iOS 9,可采用修改UIAlertController按钮颜色和字号来实现

 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"只有升级成正式店主可以提现" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureA = [UIAlertAction actionWithTitle:@"去升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self goToMememberExclusiveVC];
    }];
    [sureA setValue:HHColor(229, 0, 17) forKey:@"titleTextColor"];
    UIAlertAction *cancleA = [UIAlertAction actionWithTitle:@"再等等" style:UIAlertActionStyleDefault handler:nil];
    [cancleA setValue:HHColor(51, 51, 51) forKey:@"titleTextColor"];

    [alertVC addAction:cancleA];
    [alertVC addAction:sureA];
    [self.withVC presentViewController:alertVC animated:YES completion

如果需要对title,detail颜色,字号,文本位置修改,参考:

//修改title
  NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
  [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
  [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
[alertVC setValue:alertControllerStr forKey:@"attributedTitle"];

  //修改message
  NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
  [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
  [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
   [alertVC setValue:alertControllerMessageStr forKey:@"attributedMessage"];

你可能感兴趣的:(AlertView的简单定制)