苦逼的开发者,最终败给了一个任性的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以及按钮的字体颜色的更多相关文章

  1. 修改delphi xe6 FMX Label字体颜色

    delphi fmx的字体等设置默认与皮肤有关,用代码直接修改字体颜色等是无效的,如何才能用代码修改呢?请按以下方法就可以: 1.在Object inspector中取消StlyedSettings中 ...

  2. 【Java】点击 JButton 修改 Jlabel 的文字和字体颜色

    要求: 点击 JButton 后执行一个方法 m(比较耗时),点击时改变 JLabel 的字体和颜色,方法 m 运行结束后再次修改 JLabel 的字体和颜色. 刚开始点击,都是方法 m 运行结束后, ...

  3. ios修改textField的placeholder的字体颜色、大小

    textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...

  4. webStorm 如何修改angular中html的字体颜色

     请问一下:在html中怎么修改这个的颜色呢? (这个是ng中html的语法主要双括号里面的紫色太暗了,有时候都看不清是什么东东了)   修改方案:(说明:第三步需要把最后的选择框去掉,默认是选中的, ...

  5. ios修改textField的placeholder的字体颜色和大小

    textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...

  6. 修改textField的placeholder的字体颜色、大小

    textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...

  7. Android基础TOP2:单机按钮改变字体颜色

    ---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...

  8. 【转】PS1应用-修改linux终端命令行字体颜色

    原文链接:https://www.jianshu.com/p/4239d3ea72fe cd ls -la vim .bashrc 在.bashrc中加入这一行: PS1="\[\e[37; ...

  9. UIImagePickerController导航字体颜色和背景

    创建UIImagePickerController // 创建图片选择器 UIImagePickerController *picker = [[UIImagePickerController all ...

随机推荐

  1. jquery提示气泡

    <link href="css/manhua_hoverTips.css" type="text/css" rel="stylesheet&qu ...

  2. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  3. nginx.conf配置

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processe ...

  4. Hbase 建表基本命令总结

    访问hbase,以及操作hbase,命令不用使用分号 hbase shell 进入hbase list 查看表 hbase shell -d hbase(main):024:0> scan '. ...

  5. Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution

    #define ADDITION '+' #define SUBSTRACTION '-' #define MULTIPLICATION '*' #define DIVISION '/' class ...

  6. HDU 1074 Doing Homework(状态压缩DP)

    题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. #include<iostream& ...

  7. uvalive 5031 Graph and Queries 名次树+Treap

    题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...

  8. 【转】getopt分析命令行参数

    (一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...

  9. Scott Hanselman's 2014 Ultimate Developer and Power Users Tool List for Windows -摘自网络

    Everyone collects utilities, and most folks have a list of a few that they feel are indispensable.  ...

  10. Android-Native-Server 启动和注册详细分析

    Android-Native-Server 启动和注册详细分析     以mediaService为实例来讲解: mediaService的启动入口 是一个 传统的  main()函数 源码位置E:\ ...