UIAlertController 简单修改title以及按钮的字体颜色
苦逼的开发者,最终败给了一个任性的UI,系统原生UIAlertController的按纽颜色必须改.于是,开始了不归路.
之前的版本是自己用view写的一个仿系统UIActionSheet,动画感觉都挺好,就是毛玻璃背景没有系统的好,由于最低兼容了ios8,所以就抛弃了UIActionSheet,改用UIAlertController.
做法其实很简单,采用runtime机制.对于runtime不了解的,我想还是别看各种介绍文章了,找一个简单的demo多写几遍,就行了.
做法很简单,自己写一个类 继承自UIAlertController,还是先把.h和.m的代码都给大家吧.
SCAlertController.h
//
// SCAlertController.h
// SCAlertController
//
// Created by it3部01 on 16/8/3.
// Copyright ? 2016年 benben. All rights reserved.
// #import @interface SCAlertAction : UIAlertAction @property (nonatomic,strong) UIColor *textColor; /**< 按钮title字体颜色 */ @end @interface SCAlertController : UIAlertController @property (nonatomic,strong) UIColor *tintColor; /**< 统一按钮样式 不写系统默认的蓝色 */
@property (nonatomic,strong) UIColor *titleColor; /**< 标题的颜色 */
@property (nonatomic,strong) UIColor *messageColor; /**< 信息的颜色 */ @end
SCAlertController.m
//
// SCAlertController.m
// SCAlertController
//
// Created by it3部01 on 16/8/3.
// Copyright ? 2016年 benben. All rights reserved.
// #import "SCAlertController.h"
#import @implementation SCAlertAction //按钮标题的字体颜色
-(void)setTextColor:(UIColor *)textColor
{
_textColor = textColor; unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
for(int i =0;i < count;i ++){ Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding]; if ([ivarName isEqualToString:@"_titleTextColor"]) { [self setValue:textColor forKey:@"titleTextColor"];
}
}
} @end @implementation SCAlertController -(void)viewDidLoad
{
[super viewDidLoad]; unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
for(int i = 0;i < count;i ++){ Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding]; //标题颜色
if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && self.titleColor) { NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedTitle"];
} //描述颜色
if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) { NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedMessage"];
}
} //按钮统一颜色
if (self.tintColor) {
for (SCAlertAction *action in self.actions) {
if (!action.textColor) {
action.textColor = self.tintColor;
}
}
}
} @end
一般来说对于SCAlertController里面的属性应该像SCAlertAction一样放在setter方法里面修改,这里我表示为了方便就放在这个方法里面了.
-(void)viewDidLoad
{
[super viewDidLoad];
}
用法就很简单了,和系统原生UIAlertController一样,只是把UI换成SC,当然你可以改成自己喜欢的,但是别忘了改完.
SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你确定要退出吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alertController.tintColor = [UIColor redColor]; //这里统一设置各个按钮的颜色都为红色.
当然,你还可以自定义某一个按钮的颜色.比如下面的取消按钮
alertController.titleColor = [UIColor redColor]; //取消
SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil]; //单独修改一个按钮的颜色
cancelAction.textColor = [UIColor greenColor];
[alertController addAction:cancelAction]; //退出
SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }];
[alertController addAction:exitAction]; [self presentViewController:alertController animated:YES completion:nil];
}
UIAlertController 简单修改title以及按钮的字体颜色的更多相关文章
- 修改delphi xe6 FMX Label字体颜色
delphi fmx的字体等设置默认与皮肤有关,用代码直接修改字体颜色等是无效的,如何才能用代码修改呢?请按以下方法就可以: 1.在Object inspector中取消StlyedSettings中 ...
- 【Java】点击 JButton 修改 Jlabel 的文字和字体颜色
要求: 点击 JButton 后执行一个方法 m(比较耗时),点击时改变 JLabel 的字体和颜色,方法 m 运行结束后再次修改 JLabel 的字体和颜色. 刚开始点击,都是方法 m 运行结束后, ...
- ios修改textField的placeholder的字体颜色、大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- webStorm 如何修改angular中html的字体颜色
请问一下:在html中怎么修改这个的颜色呢? (这个是ng中html的语法主要双括号里面的紫色太暗了,有时候都看不清是什么东东了) 修改方案:(说明:第三步需要把最后的选择框去掉,默认是选中的, ...
- ios修改textField的placeholder的字体颜色和大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- 修改textField的placeholder的字体颜色、大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- 【转】PS1应用-修改linux终端命令行字体颜色
原文链接:https://www.jianshu.com/p/4239d3ea72fe cd ls -la vim .bashrc 在.bashrc中加入这一行: PS1="\[\e[37; ...
- UIImagePickerController导航字体颜色和背景
创建UIImagePickerController // 创建图片选择器 UIImagePickerController *picker = [[UIImagePickerController all ...
随机推荐
- 解锁windowsphone设备遇到的错误:检查Miscrosoft账户凭据、请重新注册 0x80004005 解决方案
本篇文章主要讲在解锁windowsphone设备时遇到的错误 Error1:登录windowsphone开发人员中心时出错,请检查您的miscrosoft账户凭据 Error2:注册您的手机时出现未知 ...
- NOI2006最大获利
终于搞明白了…… x到y有边意味着选x必须选y,所以才会有闭合子图中一个点的后继一定也在这个闭合子图中. 接下来按照s连正权,负权连t就ok了 type node=record go,next,c:l ...
- 【CSS】
12个很少被人知道的CSS事实 通过CSS禁止Chrome自动为输入框添加橘黄色边框http://www.solagirl.net/override-chromes-automatic-border- ...
- 嵌入式Linux启动过程中的问题积累
嵌入式Linux启动过程中的问题积累 Dongas 07-12-19 1.Bad Magic Number ## Booting image at 33000000 ... Bad Magic Num ...
- WCF 学习总结2 -- 配置WCF
前面一篇文章<WCF 学习总结1 -- 简单实例>一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成,省去了我们不少功夫.现在回 ...
- ubuntu设置ip和dns
装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns. 下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需. 一.配置ip ub ...
- CF GYM 100703K Word order
题意:给一个字符串,其中只有F.A.N三种字母,问最少交换多少次能使所有的A在所有F之前. 解法:贪心.先预处理每位的左边有多少F右边有多少A,对于每位A必须至少向左交换的次数为它左面的F个数,而对于 ...
- Uva11732(trie)
题意:给你n个字符串 用strcmp()两两比较 ,求字符比较的总次数 分析: 数据量很大我们考虑用孩子兄弟表示法来表示字典树 #include <cstdio> #include < ...
- HNU OJ10086 挤挤更健康 记忆化搜索DP
挤挤更健康 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 339, A ...
- 【DOM】1.DOM优化
1.JS include :DOM BOM ECMA 2.Browser 分别独立实现dom & JS as if two isolated islands 3.JS操作DOM from th ...