效果图如下:
之前看了很多的博客,都是先获取 Ivar属性,然后判断是title还是message再进行修改,其实不用,可以直接拿到title和message进行修改,代码如下:
/**
简单的文字弹窗,带’确定‘文字
@param title 题目
@param message 内容
@param target 目标控制器
*/
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message target:(nullable id)target {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleAlert];
// title 颜色
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString: title attributes: @{NSForegroundColorAttributeName: [UIColor greenColor]}];
[alertController setValue: attr forKey: @"attributedTitle"];
// message颜色
NSMutableAttributedString *attrMessage = [[NSMutableAttributedString alloc]initWithString: message attributes: @{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName:[UIFont boldSystemFontOfSize: 12.0]}];
[alertController setValue: attrMessage forKey: @"attributedMessage"];
// UIAlertAction颜色
UIAlertAction *sure = [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction: sure];
[sure setValue: [UIColor darkGrayColor] forKey: @"titleTextColor"];
[target presentViewController: alertController animated: YES completion:nil];
}
我把上面的方法封装在了的分类中,调用十分的方便,如下:
[UIAlertController showAlertWithTitle: @"标题" message: @"\n内容\n" target: self];