iOS 修改UIAlertAction的字体大小

一般来说,如果需要修改UIAlertController的标题(title)、内容(message)的字体和颜色,可以利用KVC来实现:参考链接:http://www.jianshu.com/p/51949eec2e9c
但如果需要修改UIAlertAction中的文字字体,利用KVC,获取出的属性只有能修改颜色的_titleTextColor(在iOS8.3之后出现)。

通过这篇文章:http://www.jianshu.com/p/f6752f7f8709 知道,我们可以给UILabel添加分类,修改所有出现在UIAlertController中字体的样式(这种方法不好的地方就是,所有的字体样式都改变了)。
具体代码:
UILable的分类:

#import <UIKit/UIKit.h>
@interface UILabel (AlertActionFont)
@property (nonatomic,copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
@end

#import "UILabel+AlertActionFont.h"

@implementation UILabel (AlertActionFont)
- (void)setAppearanceFont:(UIFont *)appearanceFont
{
    if(appearanceFont)
    {
        [self setFont:appearanceFont];
    }
}

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

修改样式:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
[cancelAction setValue:[Utils colorWithHexString:@"#00A7FA"] forKey:@"titleTextColor"];//iOS8.3
[alert addAction: cancelAction];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherTitles[i] style:UIAlertActionStyleDefault handler:otherBlocks[i]];
[otherAction setValue:[Utils colorWithHexString:@"#00A7FA"] forKey:@"titleTextColor"];//iOS8.3
[alert addAction: otherAction];

UILabel *appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
UIFont *font = [UIFont systemFontOfSize:13];
[appearanceLabel setAppearanceFont:font];

如何通过kvc获取key值:

//kvc 获取所有key值
- (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];
        @try {
            id valueStr = [object valueForKey:keyStr];
            NSDictionary *dic = nil;
            if (valueStr) {
                dic = @{keyStr : valueStr};
            } else {
                dic = @{keyStr : @"值为nil"};
            }
            [array addObject:dic];
        }
        @catch (NSException *exception) {}
    }
    return [array copy];
}

//获得所有属性
- (NSArray *)getAllProperty:(id)object
{
    NSMutableArray *array = [NSMutableArray array];
    
    unsigned int count;
    objc_property_t *propertys = class_copyPropertyList([object class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertys[i];
        const char *nameChar = property_getName(property);
        NSString *nameStr = [NSString stringWithCString:nameChar encoding:NSUTF8StringEncoding];
        [array addObject:nameStr];
    }
    return [array copy];
}

使用:
UILabel *label = [[UILabel alloc] init];
NSLog(@"********所有变量/值:\n%@", [self getAllIvar:label]);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 不知不觉2017年的余额已经所剩无几了 下面是我这一年来收藏的关于IOS开发的一些知识点 . iOS功能 iOS ...
    临渊还在阅读 3,980评论 0 0
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,767评论 7 249
  • “果然都不在啊!”那感情正好。 我回到寝室的时候大约已经上午十点了。对于大学生来说,周末本该是赖在寝室睡觉打游戏的...
    叶落凡天阅读 1,837评论 2 1
  • 世界上有一种炮灰,自己做的非常开心,不在乎别人的想法,只希望自己做的自己最为开心。但是,突然有一天我们被派往更佳的...
    馨凰阅读 1,715评论 0 0
  • 光,奋力逃离黑洞 无知依旧在那里 童年,追随长大的梦 回忆依旧在那里 潮汐来临的时候 贝壳,瞧见回家的希望 可沙滩...
    冰上之路阅读 3,025评论 6 10

友情链接更多精彩内容