UIAlertController 修改文字显示实现方法

UIAlertController修改文字显示

不废话先上完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
    //修改标题
    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [attrTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, [[attrTitle string] length])];
    [attrTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[attrTitle string] length])];
    [alertController setValue: attrTitle forKey:@"attributedTitle"];
    //修改message
    NSMutableAttributedString * attrMessage = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
    [attrMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [[attrMessage string] length])];
    [attrMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[attrMessage string] length])];
    [alertController setValue: attrMessage forKey:@"attributedMessage"];
    //修改按钮的颜色,同上可以使用同样的方法修改内容,样式
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [defaultAction setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
    [cancelAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
 
    [alertController addAction:defaultAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

修改标题属性

key: attributedTitle

1
2
3
4
5
//修改标题的内容,字号,颜色。使用的key值是“attributedTitle"
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"标题"];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, [[attr string] length])];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])];
    [alertController setValue:hogan forKey:@"attributedTitle"];

修改内容属性

key: attributedMessage

1
2
3
4
5
//修改message
    NSMutableAttributedString * attrMessage = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
    [attrMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [[attrMessage string] length])];
    [attrMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[attrMessage string] length])];
    [alertController setValue: attrMessage forKey:@"attributedMessage"];

标题AlertAction按钮字体颜色

key: _titleTextColor或者titleTextColor

1
2
3
4
5
6
7
8
9
//修改按钮的颜色,同上可以使用同样的方法修改内容,样式
   UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
   UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
 
   [defaultAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
   [cancelAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
 
   [alertController addAction:defaultAction];
   [alertController addAction:cancelAction];

效果图

posted @   brave-sailor  阅读(340)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2018-08-29 NSRegularExpression iOS自带的正则表达式
2017-08-29 wesome-android
2016-08-29 如何在mac本上安装android sdk
2016-08-29 Mac OS X 中安装JDK7
2016-08-29 导入一个AndroidStudio工程作为一个Library Module
2016-08-29 Android Studio导入第三方类库的方法
2014-08-29 Intent Flag介绍 intent.addFlags()
点击右上角即可分享
微信分享提示