相信很多程序员都会遇到需求是这样的:

但是你发现无论怎么设置cancel和Destructive都无法让红色字体移动到下面取消按钮上:

其实之前一直用错,用了ios9之前的UIActionSheet这个view来当控件,但是发现,UIAlertController这个控制器用来做这个更好,只需一行代码就可以搞定。以下是我自己通过利用kvc去对字体颜色做改变的做法,希望有其他见解的可以随时交流。

- (void)viewDidLoad {

[super viewDidLoad];

self.button    = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

_button.backgroundColor  = [UIColor blueColor];

[_button setTitle:@"性别" forState:UIControlStateNormal];

[_button addTarget:self action:@selector(selectSheet) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_button];

}

- (void)selectSheet

{

UIAlertAction *alertAct = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@"男" forState:UIControlStateNormal];

}];

UIAlertAction *alertAct1 = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@"女" forState:UIControlStateNormal];

}];

UIAlertAction *alertAct2 = [UIAlertAction actionWithTitle:@"保密" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@"保密" forState:UIControlStateNormal];

}];

UIAlertAction *alertAct3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

//******通过利用kvc对字体颜色进行修改 *******//

[alertAct3 setValue:[UIColor redColor] forKey:@"_titleTextColor"];

UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[alertControl addAction:alertAct];

[alertControl addAction:alertAct1];

[alertControl addAction:alertAct2];

[alertControl addAction:alertAct3];

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

}

关于UIAlertAction如何修改sheet上的字体颜色的更多相关文章

  1. 修改TabBarViewController上标题字体颜色

    UINavigationController *newNavVc = [[UINavigationController alloc]init]; newNavVc.title = title; new ...

  2. runtime查找 UIAlertAction 的key 及 UIActionSheet 设置字体颜色

    修改不了颜色了 结果发现kvo 的key 不对 哎 直接上代码 设置正确的属性找到对应的key  还以为iOS 11改变了方法 unsigned int count; Ivar *ivars =  c ...

  3. WPS修改批注部分的字体颜色?

    今天遇到一个问题,就是复制文档的时候有几块红色字体想改成黑色,怎么也改不成功,通过修改字体颜色无效,通过百度找到了解决方法记录一下. 解决方法 审阅--显示标记--点击插入和删除(去掉前面的对钩即可) ...

  4. NPOI 修改指定单元格字体颜色

    //创建一个字体颜色 IFont font = hssfworkbook.CreateFont(); //红色 font.Color = HSSFColor.Red.Index; //样式 ICell ...

  5. Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景

    有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过And ...

  6. 修改Vim内注释字体颜色

    vim /etc/vimrc   然后按大写  G 到最后一行,插入 hi comment ctermfg=6          wq保存退出 PS:默认的注释颜色是4  然后有0,1,2,3,4,5 ...

  7. ECharts修改坐标轴,坐标轴字体,坐标轴网格样式以及控制坐标轴是否显示

    转自:http://blog.csdn.net/kirinlau/article/details/72876689 首先要将一个图表显示在前端页面上: var myChart = echarts.in ...

  8. 修改UITextfield的Placeholder字体的颜色

    - (void)viewDidLoad { [super viewDidLoad]; self.title=@"修改UITextField的placeholder字体颜色"; UI ...

  9. textFiled的placeHolder字体颜色

    self.title=@"修改UITextField的placeholder字体颜色"; UITextField *textTF=[[UITextField alloc]initW ...

随机推荐

  1. python2.7 报错(UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128))

    报错: 原来用的python3.5版本后来改为2.7出现了这个错误里面的中文无法显示 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 ...

  2. 【JS】字符串操作

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  3. 常用jQuery代码

    返回元素的html包括它本身 <div class="test"><p>hello,你好!</p></div> <script ...

  4. MVC安全防护

    一.XSS攻击 跨域脚本攻击(Cross Site Scripting),恶意植入前端代码,比如HTML代码和客户端脚本,异常js获取用户cookie然后跳转到别的站点. 防护措施 标签转换(如&qu ...

  5. connect/express 的参考

    1.Node.js[5] connect & express简介    对connect中间件的分类比较容易理解. http://www.cnblogs.com/luics/archive/2 ...

  6. Debian/Ubuntu server上安装安全更新

    原始链接:http://serverfault.com/questions/270260/how-do-you-use-apt-get-to-only-install-critical-securit ...

  7. windows+ant+git+tomcat中ant直接获取git项目部署注意点

    最近项目搬迁到公司的"GitHub"上面原来的SVN的ant发布脚本要改下,于是百度ant获取git的方法太少了,windows平台上更是没有所以搞了两天,今天终于有点成果分享给大 ...

  8. cglib动态新增类方法

    <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> & ...

  9. Linux下memcache的安装和启动

    memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...

  10. sublime如何自动保存

    sublime是前端开发者喜欢使用的工具,它有很多快捷方式可以让我们快速的编写代码:在开发过程中,每次修改代码之后都要按Ctrl+S保存.在这里向大家介绍一下如何设置让它自动保存. 一.打开subli ...