iOS Mark

iOS实现修改UIAlertController UIAlert

2016-11-29  本文已影响346人  BigBagFind

Alert,项目常用的东西,为了使用起来代码更简单,封装!
本文涉及最直接的问题:
1.UIAlertController和UIAlertView的默认属性修改,如Title,Message的textColor、font等
2.实现封装UIAlertView和UIAlertController,二合一适配系统版本

// 获得所有ivar
-(NSArray *)getAllIvar:(id)object {
    NSMutableArray *array = [NSMutableArray array];
   unsigned int count;
    Ivar *ivars = class_copyIvarList([object class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char *keyChar = ivar_getName(ivar);
        NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
        id valueStr = [object valueForKey:keyStr];
        NSDictionary *dic = nil;
        if (valueStr) {
            dic = @{keyStr : valueStr};
        } else {
            dic = @{keyStr : @"值为nil"};
        }
        [array addObject:dic];
    }
    return [array copy];
    }
    return [array copy];
}
// UIAlertController
  "_cancelAction",
    preferredStyle,
    attributedTitle,
    attributedMessage,
// UIAlertView
 "_alertController" = "<UIAlertController: 0x7fc712c07410>";

1.看到attributed这个单词,相信大家大概都懂了,想到NSAttributedString那么就很开放了,字体大小,颜色,其他样式随便设置,问题迎刃而解。
2.发现UIAlertView里面带的是_alertController,原来苹果早就在内部改变了AlertView,里面并没有contentView,全丢到这个私有属性里了,所以要修改AlertView样式还是在修改内部的AlertController,私有属性同上
3._cancelAction,还可以往里深入,继续打印探究,改变其颜色字体等

// 当然你也可以往attributedStringadd加一些颜色和其他样式
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
[ps setAlignment:NSTextAlignmentLeft];
[alertControllerMessageStr addAttribute:NSParagraphStyleAttributeName value:ps range:NSMakeRange(0, message.length)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, message.length)];
 [alertVc setValue:alertControllerMessageStr forKey:@"attributedMessage"];
UIAlertController *alertVc = [alertView valueForKey:@"alertController"];
 NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
 [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, message.length)];
 [alertVc setValue:alertControllerMessageStr forKey:@"attributedMessage"];
// 弹出AlertView带左对齐Message,
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                                  LeftTextmessage:(NSString *)message
                              firstAction:(NSString *)firstAction
                             secondAction:(NSString *)secondAction
                             successBlock:(SuccessBlock)successBlock;
// 弹出AlertView就一个action,无message无block
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                              actionTitle:(NSString *)actionTitle;
// 弹出AlertView就一个action,无message
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                              actionTitle:(NSString *)actionTitle
                             successBlock:(SuccessBlock)successBlock;
// 弹出AlertView就一个action,无title无block
-(void)tgg_presentAlertViewWithMessage:(NSString *)message
                            actionTitle:(NSString *)actionTitle;
// 弹出AlertView就一个action,无title
-(void)tgg_presentAlertViewWithMessage:(NSString *)message
                            actionTitle:(NSString *)actionTitle
                           successBlock:(SuccessBlock)successBlock;
// 弹出AlertView就一个action没有block
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                                  message:(NSString *)message
                              actionTitle:(NSString *)actionTitle;
// 弹出AlertView就一个action的基础方法,带block回调
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                                  message:(NSString *)message
                              actionTitle:(NSString *)actionTitle
                             successBlock:(SuccessBlock)successBlock;
// 弹出AlertView的基本方法,最多2个action
-(void)tgg_presentAlertViewWithMainTitle:(NSString *)mainTitle
                                  message:(NSString *)message
                              firstAction:(NSString *)firstAction
                             secondAction:(NSString *)secondAction
                             successBlock:(SuccessBlock)successBlock;
上一篇下一篇

猜你喜欢

热点阅读