https://www.jianshu.com/p/51949eec2e9c

2016.03.23 22:36* 字数 272 阅读 37401评论 54喜欢 72

在开发中,弹出框是必不可少的,通常情况下,我们只要弹出系统自带的弹出框就可以。but,在某些情况下,万恶的UI会要求你修改显示文字的大小、颜色,虽然系统自带有一种红色字体的UIAlertAction,但是这种Action并不能放在Cancel位置,所以,更多时候,需要我们自己修改文字字体和颜色。
我采用的方法是KVC:
正常情况下,我们配置出来的UIAlertController是这样的:

 
123.png

或者是这样:

 
345.png

代码如下:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];

//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];

UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:nil];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

[alertController addAction:defaultAction];

[alertController addAction:destructiveAction];

[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];

代码里展示了系统提供的三种UIAlertAction,现在我们要对文字的字体和颜色进行设置:

  • 1.标题和提示内容的文字设置
    代码如下:

//修改title

NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];

[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];

[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];

[alertController setValue:alertControllerStr forKey:@"attributedTitle"];

//修改message

NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];

[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];

[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];

[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];

效果如下:

 
123.png
  • 2.设置按钮文字,就拿取消按钮距离:
    代码如下:

//修改按钮

if (cancelAction valueForKey:@"titleTextColor") {

[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

}

效果如下:

 
123.png

至于里面的key值怎么得到的,过两天会写一篇文章来讲述。

经常需要设置message左对齐、段间距这些属性

- (IBAction)tapAlertControllerButton:(id)sender {
NSString *titleString = @"商家发货规则";
NSString *messageString = @"1、买家下单付款后,请尽快发货;\n2、买家下单付款后,若超过最迟发货的时间,商家仍未发货,那么后台将自动取消订单;\n3、若遇到物流高峰期,比如春节,双十一等,请点击【延迟发货】来延长最迟发货时间;\n4、每个订单只允许【延迟发货】一次,每次可延迟5天;";
NSString *alertTitle = @"知道了";

UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:titleString message:messageString preferredStyle:UIAlertControllerStyleAlert];
[_alertController addAction:[UIAlertAction actionWithTitle:alertTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}]];

/* 修改title */
NSMutableAttributedString *attTitleString = [[NSMutableAttributedString alloc] initWithString:titleString];
[_alertController setValue:attTitleString forKey:@"attributedTitle"];

/* 修改message */
NSMutableAttributedString *attMsgString = [[NSMutableAttributedString alloc] initWithString:messageString];
// 设置字体
[attMsgString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attMsgString.length)];
// 设置颜色
[attMsgString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
// 设置行间距
[paragraph setLineSpacing:3];
// 设置段间距
[paragraph setParagraphSpacingBefore:5];
// 设置对齐方式
[paragraph setAlignment:NSTextAlignmentLeft];
// 设置书写方向
[paragraph setBaseWritingDirection:NSWritingDirectionLeftToRight];
[attMsgString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attMsgString.length)];
[_alertController setValue:attMsgString forKey:@"attributedMessage"];

/* 修改按钮的颜色 */
NSArray *actionArr = [_alertController actions];
[actionArr.firstObject setTitle:alertTitle];
[actionArr.firstObject setValue:[UIColor orangeColor] forKey:@"titleTextColor"];

[self presentViewController:_alertController animated:YES completion:nil];
}
 
示意图:
系统默认样式:

改变属性样式:


---------------------
作者:枫志应明
来源:CSDN
原文:https://blog.csdn.net/wsyx768/article/details/60874420
版权声明:本文为博主原创文章,转载请附上博文链接!

[iOS]改变UIAlertController的标题、内容的字体和颜色的更多相关文章

  1. iOS导航栏背景,标题和返回按钮文字颜色

    在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...

  2. Android(java)学习笔记96:如何改变spinner系统自带的字体和颜色

    1.首先我们要知道spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spinn ...

  3. Android(java)学习笔记35:如何改变Spinner系统自带的字体和颜色

    1. 首先我们要知道Spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spin ...

  4. iOS开发笔记-一种任意字体、颜色混排UILabel的实现

    最近开发新App,射妓狮给的图上出现一种不同大小字体混排的Label,就像下面这种: 想了想,最简单的方法是使用多个UILabel排列显示,但是这样不仅麻烦而且效果也不好,索性自定义UILabel来尽 ...

  5. IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容

    IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容 UIAlertController *alertVC = [UIAlertController alertControl ...

  6. iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

    废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...

  7. IOS 改变导航栏返回按钮的标题

    IOS 改变导航栏返回按钮的标题   下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...

  8. iOS 最新UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...

  9. iOS学习——UIAlertController详解

    在开发中,弹出提示框是必不可少的.这两天项目中统一对已经被iOS API废弃的UIAlertView和UIActionSheet进行替换,我们知道,UIAlertView和UIActionSheet都 ...

随机推荐

  1. Microsoft SQL Server sa 账户 登录错误18456

    分析:在安装Sql server 2012的时候,服务器身份验证没有选择“SQL Server 和 Windows身份验证模式(S)”,导致SQL Server身份验证方式被禁用. 操作: 以Wind ...

  2. jQuery -- 光阴似箭(一):初见 jQuery -- 基本用法,语法,选择器

    jQuery -- 知识点回顾篇(一):初见jQuery -- 基本用法,语法,选择器 1. 使用方法 jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. ...

  3. LeetCode算法题-Excel Sheet Column Title(Java实现)

    这是悦乐书的第180次更新,第182篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第39题(顺位题号是168).给定正整数,返回Excel工作表中显示的相应列标题.例如: ...

  4. Hadoop下添加节点和删除节点

    添加节点 1.修改host   和普通的datanode一样.添加namenode的ip 2.修改namenode的配置文件conf/slaves   添加新增节点的ip或host 3.在新节点的机器 ...

  5. ubuntu如何安装 adobe flash player或adobe插件

    方法/步骤 第一步当然是打开终端控制器.有很多方法,这里推荐使用快捷键:ctrl+alt+T.快捷又方便. 然后更新源列表,使用如下命令:sudo apt-get update,后面要输入密码. 下面 ...

  6. Win7 下安装ubuntu14.04双系统

    下面介绍一下利用wubi在Windows中安装Ubuntu 14.04的教程,或者说安装方法和注意事项.  方法一:直接下载wubi.exe 方法二:直接下载ubuntu-14.04-desktop- ...

  7. ubuntu下定时任务的执行

    概述 linux系统由 cron (crond) 这个系统服务来控制例行性计划任务.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的. 另外, 由于使用者自己也可以设置计划 ...

  8. Loj #6069. 「2017 山东一轮集训 Day4」塔

    Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...

  9. .Net下的全局异常捕获问题

    全局异常捕获主要目标并不是为了将异常处理掉防止程序崩溃.因为当错误被你的全局异常捕获器抓到的时候,已经证实了你程序中存在BUG. 一般而言,我们的全局异常捕获主要作用就是接收到异常之后进行异常的反馈. ...

  10. UVA10410-Tree Reconstruction(BFS序和DFS序的性质)

    Problem UVA10410-Tree Reconstruction Accept:708  Submit:4330 Time Limit: 3000 mSec Problem Descripti ...