源码

首页 » 归档 » 源码 » UIAlertController 简单修改title以及按钮的字体颜色-ios学习从入门到精通尽在姬长信

UIAlertController 简单修改title以及按钮的字体颜色-ios学习从入门到精通尽在姬长信

分享最热门的ios资讯

苦逼的开发者,最终败给了一个任性的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];
    }

文章转自 青春微凉来时路的简书
用意志战胜身体的惰性!

(0)

本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/1190.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 14, 2016 at 07:59 下午

关键词:

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复