iOS开发中利用系统的UIAlertController自定义弹窗

使用系统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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容