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 ...
随机推荐
- [LOJ 1038] Race to 1 Again
C - Race to 1 Again Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- js变量作用域
for(var i =0;i<100;i++) { } alert(i);//100 if(true){ var i="91d"; } alert(i);//91d func ...
- MYSQL select时锁定记录问题
在使用SQL时,大都会遇到这样的问题,你Update一条记录时,需要通过Select来检索出其值或条件,然后在通过这个值来执行修改操作. 但当以上操作放到多线程中并发处理时会出现问题:某线程selec ...
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- HDU 4968 Improving the GPA
Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.4
(1). There is a natural isomorphism between the spaces $\scrH\otimes \scrH^*$ and $\scrL(\scrH,\scrK ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.7
For every matrix $A$, the matrix $$\bex \sex{\ba{cc} I&A\\ 0&I \ea} \eex$$ is invertible and ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- Kooboo CMS的安装步骤
Kooboo CMS的安装步骤 来自Kooboo document 跳转到: 导航, 搜索 http://www.microsoft.com/web/gallery/install.aspx?appi ...
- C# 两个ListBox 数据互传-基础操作
先看效果图: 两个服务设施列,左边:lbFacility1,右边:lbFacility2,中间向左向右箭头. 如果只是单纯的向左向右移动,那很简单. 因为项目遇到要获取选中项的ID,通过给ListBo ...