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];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM