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的字体颜色呢?

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 <objc/runtime.h>
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
for (int i = 0; i<count; i++) {
// 取出成员变量
// Ivar ivar = *(ivars + i);
Ivar ivar = ivars[i];
// 打印成员变量名字
NSLog(@"%s------%s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}

获取到对应的属性我们就可以修改系统内部的默认值了

// 取消按钮
-(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];

posted @ 2016-07-29 10:38  GJR  阅读(1842)  评论(0编辑  收藏  举报