使用系统UIAlertController自定义弹窗的最大好处是:不需要处理文本的显示、无需手动管理弹窗的生命周期,无需更改弹窗的显示层级。
如果不进行封装的话,会在每个使用点单独进行创建,重复代码过多,不利于开发工作推进。
可修改的内容包括:富文本的title及message、按钮的数量、按钮的文字颜色。
使用方法:
NSString *title = @"这是标题";
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:title];
[attributedTitle addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, attributedTitle.length)];
[attributedTitle addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, attributedTitle.length)];
NSString *message = @"这是内容";
NSMutableAttributedString *attributedMessage = [[NSMutableAttributedString alloc] initWithString:message];
[attributedMessage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedMessage.length)];
[attributedMessage addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, attributedMessage.length)];
[[ZKAlertManager shared] showWithTitle:attributedTitle message:attributedMessage cancelText:@"" cancelColor:nil doneText:@"确定" doneColor:[UIColor redColor] controller:self cancelButtonHandler:^{
NSLog(@"点击了取消");
} doneButtonHandler:^{
NSLog(@"点击了确定");
}];
代码地址:https://github.com/ZKhercules/ZKAlertManager