UIAlertController

2016-08-07  本文已影响25人  wpf_register

自定义类 .h 文件

#import <UIKit/UIKit.h>

@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

自定义类 .m 文件

#import "SCAlertController.h"
#import <objc/runtime.h>

@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"];
        }
    }

    //按钮如果没设颜色,则统一用tintColor
    if (self.tintColor) {
        for (SCAlertAction *action in self.actions) {
            if (!action.textColor) {
                action.textColor = self.tintColor;
            }
        }
    }
}

@end

用法和父类的用法一致

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:nil];
[alertController addAction:exitAction];

 [self presentViewController:alertController animated:YES completion:nil];
+(void)alertShowTitle:(NSString*)title
               message:(NSString*)message
              sureText:(NSString*)sureText
             sureBlock:(void(^)())sureBlock
            cancelText:(NSString*)cancelText
           cancelBlock:(void(^)())cancelBlock
         alertVCsender:(UIViewController*)sender{

    if (!sender) {
       sender = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    
    UIAlertController *alertController =[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (sureBlock) {
             sureBlock();
        }
      }];
    [alertController addAction:sureAction];
    
    UIAlertAction *cancelAction = nil;
    if (cancelText.length > 0) {
      cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (cancelBlock) {
                cancelBlock();
            }
        }];
    [alertController addAction:cancelAction];
    }
    
    [sender presentViewController:alertController animated:YES completion:nil];
    
}
上一篇下一篇

猜你喜欢

热点阅读