iOS更改UIActionController弹出字体的样式

最近,项目遇到需求,需要更改底部弹出框字体的样式,实现效果如下:


iOS更改UIActionController弹出字体的样式_第1张图片
效果图

具体实现思路如下:
在此需要说明一下:在iOS8.0之前,我们可以使用UIActionSheet,重写- (void)willPresentActionSheet:(UIActionSheet *)actionSheet,此方法可实现更改其内部的字体样式
但是在iOS8.0之后,利用上述方法则不行了,苹果使用UIAlertController代替了UIActionSheet和UIAlertView,但是在UIAlertController头文件中,我们并不能看到直接可以修改内部字体的属性,因此可利用运行时获取UIAlertAction内部的属性,但是获取出的属性只有能修改颜色的[_titleTextColor],没有修改字体大小的属性,这时我们可以给UILabel添加分类,修改所有出现在UIActionController中字体的样式,具体实现代码如下:

// 运行时获取属性代码

        unsigned int count = 0;
        Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
        for (int i = 0; i

// 给UILabel定义分类JJKAlertActionFont

@interface UILabel (JJKAlertActionFont)
/** font */
@property (nonatomic,copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
@end

#import "UILabel+JJKAlertActionFont.h"
@implementation UILabel (JJKAlertActionFont)
- (void)setAppearanceFont:(UIFont *)appearanceFont
{
    if(appearanceFont)
    {
        [self setFont:appearanceFont];
    }
}

- (UIFont *)appearanceFont
{
    return self.font;
}
@end

// 底部弹框代码

@property (nonatomic,strong) UIActionSheet *actionSheet;
#pragma mark - 懒加载
- (UIActionSheet *)actionSheet
{
    if(_actionSheet == nil)
    {
        _actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"早餐前", @"早餐后", @"午餐前", @"午餐后", @"晚餐前", @"晚餐后", @"睡前", @"凌晨", nil];
    }
    
    return _actionSheet;
}

// 选择记录的类型
- (IBAction)btnRecordTime:(UIButton *)sender {
    if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        NSArray *titles = @[@"早餐前", @"早餐后", @"午餐前", @"午餐后", @"晚餐前", @"晚餐后", @"睡前", @"凌晨"];
        [self addActionTarget:alert titles:titles];
        [self addCancelActionTarget:alert title:@"取消"];
        
        // 会更改UIAlertController中所有字体的内容(此方法有个缺点,会修改所以字体的样式)
        UILabel *appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
        UIFont *font = [UIFont systemFontOfSize:15];
        [appearanceLabel setAppearanceFont:font];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    else
    {
        [self.actionSheet showInView:self.view];
    }
}

// 添加其他按钮
- (void)addActionTarget:(UIAlertController *)alertController titles:(NSArray *)titles
{
    for (NSString *title in titles) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self.btnType setTitle:action.title forState:UIControlStateNormal];
        }];
        [action setValue:[UIColor colorWithHexColor:@"#2a2a2a"] forKey:@"_titleTextColor"];
        [alertController addAction:action];
    }
}

// 取消按钮
- (void)addCancelActionTarget:(UIAlertController *)alertController title:(NSString *)title
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    [action setValue:[UIColor colorWithHexColor:@"#7d7d7d"] forKey:@"_titleTextColor"];
    [alertController addAction:action];
}

// iOS8.0之前可用
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *view in actionSheet.subviews) {
        UIButton *btn = (UIButton *)view;
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        if([[btn titleForState:UIControlStateNormal] isEqualToString:@"取消"])
        {
            [btn setTitleColor:[UIColor colorWithHexColor:@"#7d7d7d"] forState:UIControlStateNormal];
        }
        else
        {
            [btn setTitleColor:[UIColor colorWithHexColor:@"#2a2a2a"] forState:UIControlStateNormal];
        }
    }
}

你可能感兴趣的:(iOS更改UIActionController弹出字体的样式)