iOS8.0 UIActionSheet的字体的颜色设置

在我们app的开发过程经常会遇到各种弹框,提示框的需求,然而系统的自带的字体颜色是如此的ugly。那我们今天就讨论一下,在ios7.0和8.0上面如何修改弹框的的title的字体颜色。
  • 首先在iOS7.0修改UIActionSheet title的字体是很简单的,设置代理,在willPresentActionSheet方法中修改。代码如下:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
   for (UIView *subViwe in actionSheet.subviews) {
       if ([subViwe isKindOfClass:[UILabel class]]) {
           UILabel *label = (UILabel *)subViwe;
           label.font = [UIFont systemFontOfSize:16];
           label.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMinY(label.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame)+20);
       }
       if ([subViwe isKindOfClass:[UIButton class]]) {
           UIButton *button = (UIButton*)subViwe;
           if ([button.titleLabel.text isEqualToString:@"确定"]) {
               [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
           } else {
               [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
           }
           button.titleLabel.font = [UIFont systemFontOfSize:18];
       }
   }
}

当然自从xcode在系统8.0采用了UIAlertController过后,7.0的方法就没有用了,那么在iOS8.0过后我们怎么修改title的字体颜色呢?在这里推荐一款很好用的第三方里脊串的MMPopupView 里面的控件我觉得还是很好用的!那我就不想用第三方,只想在系统方法上修改怎么办呢。很简单,下面贴上我的代码!也就几段代码,我就不上传我的github了。

  • iOS8.0 系统UIAlertController创建actionSheet的方法
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction: [UIAlertAction actionWithTitle: @"USD($)" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
    [alertController addAction: [UIAlertAction actionWithTitle:@"RMB(¥)" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){}]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
    [self presentViewController: alertController animated: YES completion: nil];
  • 首先通过runtime获取对应的内部属性
包含头文件#import 
   unsigned int count = 0;
   Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
   for (int i = 0; i
  • 获取到对应的属性我们就可以修改系统内部的默认值了

    // 取消按钮
      -(void)addCancelActionTarget:(UIAlertController*)alertController title:(NSString *)title
    

{
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

}];
[action setValue:[UIColor purpleColor] forKey:@"_titleTextColor"];
[alertController addAction:action];

}
//添加对应的title 这个方法也可以传进一个数组的titles 我只传一个是为了方便实现每个title的对应的响应事件不同的需求不同的方法

  • (void)addActionTarget:(UIAlertController *)alertController title:(NSString *)title color:(UIColor *)color action:(void(^)(UIAlertAction *action))actionTarget
    {
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    actionTarget(action);
    }];
    [action setValue:color forKey:@"_titleTextColor"];
    [alertController addAction:action];
    }

-  最后具体的实现代码就是这样的 大家可以复制代码自己去试试

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[self addActionTarget:alert title:@"星期一" color: [UIColor redColor] action:^(UIAlertAction *action) {
    NSLog(@"nicaicai");
}];
[self addActionTarget:alert title:@"星期二" color: [UIColor redColor] action:^(UIAlertAction *action) {
    NSLog(@"nicaicai");
}];

[self addActionTarget:alert title:@"星期三" color: [UIColor redColor] action:^(UIAlertAction *action) {
    NSLog(@"nicaicai");
}];

[self addActionTarget:alert title:@"星期四" color: [UIColor redColor] action:^(UIAlertAction *action) {
    NSLog(@"nicaicai");
}];
[self addCancelActionTarget:alert title:@"取消"];
[self presentViewController:alert animated:YES completion:nil];

     ##实现效果如下
![F9D4E954-DE0F-4624-B976-C9FB8BD86505.png](http://upload-images.jianshu.io/upload_images/1292550-281c9cd7ac5cabf9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(iOS8.0 UIActionSheet的字体的颜色设置)