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 ...
随机推荐
- 函数 buf_chunk_init
http://www.tuicool.com/articles/3QbYfm http://www.360doc.com/content/13/1216/17/12904276_337644353.s ...
- 函数buf_LRU_free_block
/******************************************************************//** Try to free a block. If bpag ...
- Scrum 体验活动笔记
2014-03-10 Isoftstone library 1.识别角色(用户),形象 :名称.痛处.属性.需求 2.编写故事 story以验证需求是否正确:我们假设(客户) 进行验证结果... ...
- 统一Matlab下不同子图的色标colorbar
Reference:http://www.mathworks.com/matlabcentral/answers/100950-how-can-i-have-a-standard-colorbar-f ...
- InnoDB关键特性之doublewrite
部分写失效 想象这么一个场景,当数据库正在从内存向磁盘写一个数据页时,数据库宕机,从而导致这个页只写了部分数据,这就是部分写失效,它会导致数据丢失.这时是无法通过重做日志恢复的,因为重做日志记录的是对 ...
- C#多线程下载一个文件
这里只是说明多线程下载的理论基础,嘿嘿,并没有写多线程下载的代码,标题党了,但是我相信,看完这个代码就应该能够多线程的方式去下载一个文件了. 多线程下载是需要服务器支持的,这里并没有判断服务器不支持的 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9
(1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- SimpleHttpServer的学习之总体架构
http://www.cnblogs.com/hansongjiang/p/4213491.html 从代码中我们看到SimpleHttpServer这个类直接依赖4个类,但是最重要的类,当属其属性, ...
- A Tour of Go Interfaces are satisfied implicitly
A type implements an interface by implementing the methods. There is no explicit declaration of inte ...