一般來說,如果需要修改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]);