iOS 修改UIAlertAction的字體大小

一般來說,如果需要修改UIAlertController的標(biāo)題(title)、內(nèi)容(message)的字體和顏色,可以利用KVC來實(shí)現(xiàn):參考鏈接:http://www.itdecent.cn/p/51949eec2e9c
但如果需要修改UIAlertAction中的文字字體,利用KVC,獲取出的屬性只有能修改顏色的_titleTextColor(在iOS8.3之后出現(xiàn))。

通過這篇文章:http://www.itdecent.cn/p/f6752f7f8709 知道,我們可以給UILabel添加分類,修改所有出現(xiàn)在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]);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 不知不覺2017年的余額已經(jīng)所剩無幾了 下面是我這一年來收藏的關(guān)于IOS開發(fā)的一些知識(shí)點(diǎn) . iOS功能 iOS ...
    臨淵還在閱讀 763評(píng)論 0 0
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,005評(píng)論 7 249
  • “果然都不在?。 蹦歉星檎?。 我回到寢室的時(shí)候大約已經(jīng)上午十點(diǎn)了。對(duì)于大學(xué)生來說,周末本該是賴在寢室睡覺打游戲的...
    葉落凡天閱讀 308評(píng)論 2 1
  • 世界上有一種炮灰,自己做的非常開心,不在乎別人的想法,只希望自己做的自己最為開心。但是,突然有一天我們被派往更佳的...
    馨凰閱讀 263評(píng)論 0 0
  • 光,奮力逃離黑洞 無知依舊在那里 童年,追隨長(zhǎng)大的夢(mèng) 回憶依舊在那里 潮汐來臨的時(shí)候 貝殼,瞧見回家的希望 可沙灘...
    冰上之路閱讀 433評(píng)論 6 10

友情鏈接更多精彩內(nèi)容