美文网首页动画Swift&Objective-CiOS学习开发
UIAlertController 简单修改title以及按钮的

UIAlertController 简单修改title以及按钮的

作者: 青春微凉来时路 | 来源:发表于2016-12-15 20:18 被阅读302次

欢迎加入 iOS开发QQ群:151133690

本篇内容是对<a href ="http://www.jianshu.com/p/cecd1b4bbf27" > UIAlertController 简单修改title以及按钮的字体颜色 </a>的加强版,对系统UIAlertController类添加Category使用更加简单,同时添加block回调方法

/**
 根据otherActionTitles数组创建UIAlertController

 @param title title
 @param message message
 @param cancelActionTitle 取消Action标题
 @param otherActionTitles 其他Action标题
 @param handle 点击回调 -1 取消Action点击 0~n 其他Action点击
 @return UIAlertController
 */
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger index))handle;

先来看看效果示例

修改按钮颜色.PNG 修改标题颜色.PNG 修改按钮和标题的颜色.PNG

做法很简单,自己写UIAlertController的Category 别忘了导入头文件,然后复制如下代码到.h 和.m即可.

UIAlertController+Color.h

//
//  UIAlertController+Color.h
//  UIAlertControllerColor
//
//  Created by benben on 16/12/15.
//  Copyright © 2016年 benben. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIAlertController (Color)

@property (nonatomic,strong) UIColor *tintColor; /**< 统一按钮样式 不写系统默认的蓝色 */
@property (nonatomic,strong) UIColor *titleColor; /**< 标题的颜色 */
@property (nonatomic,strong) UIColor *messageColor; /**< 信息的颜色 */


/**
 根据otherActionTitles数组创建UIAlertController

 @param title title
 @param message message
 @param cancelActionTitle 取消Action标题
 @param otherActionTitles 其他Action标题
 @param handle 点击回调 -1 取消Action点击 0~n 其他Action点击
 @return UIAlertController
 */
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger index))handle;

@end

@interface UIAlertAction (Color)

@property (nonatomic,strong) UIColor *textColor; /**< 按钮title字体颜色 */

@end

UIAlertController+Color.m

//
//  UIAlertController+Color.h
//  UIAlertControllerColor
//
//  Created by benben on 16/12/15.
//  Copyright © 2016年 benben. All rights reserved.
//

#import "UIAlertController+Color.h"
#import <objc/runtime.h>

@implementation UIAlertController (Color)

/**
 根据otherActionTitles数组创建UIAlertController
 
 @param title title
 @param message message
 @param cancelActionTitle 取消Action标题
 @param otherActionTitles 其他Action标题
 @param handle 点击回调 -1 取消Action点击 0~n 其他Action点击
 @return UIAlertController
 */
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message cancelActionTitle:(NSString *)cancelActionTitle otherActionTitles:(NSArray<NSString *> *)otherActionTitles handle:(void (^)(NSInteger))handle
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    
    //如果有取消按钮
    if (cancelActionTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelActionTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            if (handle) {
                handle(-1);
            }
        }];
        
        [alertController addAction:cancelAction];
    }
    
    if (otherActionTitles.count > 0) {
        
        [otherActionTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (handle) {
                    handle(idx);
                }
            }];
            
            [alertController addAction:otherAction];
            
        }];
    }
    
    return alertController;
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    //按钮统一颜色
    if (self.tintColor) {
        for (UIAlertAction *action in self.actions) {
            if (!action.textColor || action.style != UIAlertActionStyleDestructive) {
                action.textColor = self.tintColor;
            }
        }
    }
}

-(UIColor *)tintColor
{
    return objc_getAssociatedObject(self, @selector(tintColor));
}

-(void)setTintColor:(UIColor *)tintColor
{
    
    objc_setAssociatedObject(self, @selector(tintColor), tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIColor *)titleColor
{
    return objc_getAssociatedObject(self, @selector(titleColor));
}

-(void)setTitleColor:(UIColor *)titleColor
{
    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 && titleColor) {
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
            [self setValue:attr forKey:@"attributedTitle"];
        }
    }
    
    free(ivars);
    
    objc_setAssociatedObject(self, @selector(titleColor), titleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIColor *)messageColor
{
    return objc_getAssociatedObject(self, @selector(messageColor));
}

-(void)setMessageColor:(UIColor *)messageColor
{
    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:@"_attributedMessage"] && self.message && messageColor) {
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
            [self setValue:attr forKey:@"attributedMessage"];
        }
    }
    
    free(ivars);
    
    objc_setAssociatedObject(self, @selector(messageColor), messageColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end


@implementation UIAlertAction (Color)
-(UIColor *)textColor
{
    return objc_getAssociatedObject(self, @selector(textColor));
}

//按钮标题的字体颜色
-(void)setTextColor:(UIColor *)textColor
{
    if (self.style == UIAlertActionStyleDestructive) {
        return;
    }
    
    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"];
        }
    }
    free(ivars);
    
    objc_setAssociatedObject(self, @selector(textColor), textColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

用法就很简单了,和系统原生UIAlertController一样

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你确定退出吗" message:@"真的要退出吗?" preferredStyle:UIAlertControllerStyleActionSheet];
    
    alertController.titleColor = [UIColor redColor];//修改title的颜色
    alertController.messageColor = [UIColor yellowColor]; //修改message的颜色
    
    alertController.tintColor = [UIColor redColor]; //全局修改Action的颜色 -- 当然你也可以单独修改某一个Action的颜色 看下面👇
    
    //添加返回按钮
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancel];
    
    cancel.textColor = [UIColor redColor]; //单个修改Action的颜色
    
    //添加确定退出按钮
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:alertAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

如果你比较懒 那么我推荐你这么使用

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"我是标题" message:@"我是message" cancelActionTitle:@"我是取消按钮" otherActionTitles:@[@"title1",@"title2",@"title3"] handle:^(NSInteger index) {
        
        //Action点击回调
        //注意 cancelAction index 为 -1,
        // otherAction index为 otherActionTitles数组下标
        
    }];
    alertController.titleColor = [UIColor redColor];//修改title的颜色
    alertController.messageColor = [UIColor yellowColor]; //修改message的颜色
    alertController.tintColor = [UIColor redColor]; //全局修改Action的颜色
    //注意 该方法 不能单独设置 某一个Action的颜色 只能修改全部
    
    alertController.tintColor = [UIColor redColor];
    [self presentViewController:alertController animated:YES completion:nil];

好了 ,就这些了...

相关文章

网友评论

  • 咔客: UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入六位纯数字" preferredStyle:UIAlertControllerStyleAlert];

    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);

    if ([weakAlert.textFields.lastObject.text isEqualToString: weakAlert.textFields.firstObject.text]) {

    _textOne = [weakAlert.textFields.firstObject text];
    _textToo = [weakAlert.textFields.lastObject text];

    [self textYesButton];

    }else
    {
    NSLog(@"错误----");
    }

    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    NSLog(@"点击了取消按钮");
    }]];

    // 添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textFieldOne) {
    textFieldOne.placeholder =@"Password";

    // [textFieldOne addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textFieldToo) {

    textFieldToo.placeholder =@"Password";
    textFieldToo.secureTextEntry = YES;

    }];


    [self presentViewController:alert animated:YES completion:nil];

    这样写的怎么更改系统自带蓝色的确认,取消按钮的颜色?
    咔客:@青春微凉来时路 ok
    青春微凉来时路:@968618715750 直接设置 alert.tintColor = [uicolor redColor];// 你想要的颜色就行

本文标题:UIAlertController 简单修改title以及按钮的

本文链接:https://www.haomeiwen.com/subject/aipgmttx.html