个性化 UIAlertController

系统的 UIAlertController 封装的很漂亮,用block代替之前 UIAlertView 的代理,用起来更方便的,但是其曝露出来的接口也不多如果要个性化一些东西,比如字体大小、颜色就不是很方便了,下面总结一下 UIAlertController 更改字体的方法,以作备忘:

1、UIAlertController 的标题(Title)、和描述(Message) 可以更改字体的大小和颜色

NSAttributedString *attTitle = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : [UIColor colorWithHexstring:@"333333"]}];
    [alert setValue:attTitle forKey:@"attributedTitle"];
NSAttributedString *attMessage = [[NSAttributedString alloc] initWithString:message attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : [UIColor colorWithHexstring:@"333333"]}];
        [alert setValue:attMessage forKey:@"attributedMessage"];

这样可以设置 alert 的title,和 message 。注意会覆盖 alert 初始化时填写的值。

2、UIAlertController 的按钮(action) 可以更改字体颜色,字体大小没找到怎么改

UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
[sureAction setValue:[UIColor colorWithHexstring:@"18a74f"] forKey:@"titleTextColor"];

 

posted @ 2016-04-25 11:08  沈红榜  阅读(275)  评论(0编辑  收藏  举报