iOS 8.0以后新出的UIAlertController ,由于系统给的字体和样式不满足需求,而我们只需要更改其字体(和app其他控件用的字体一致)和颜色,又不想自定义,于是百度和Google各种搜,终于找到想要的,在此感谢热心分享的网友,其中有些纰漏处,特写下此文补充和更正,以分享给各位网友
修改字体颜色可以分别修改
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"头像选择" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction * camerAction = [UIAlertAction actionWithTitle:@"拍一张" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[cancelAction setValue:UIColorFromRGB(0xA9BD22) forKey:@"_titleTextColor"];
[camerAction setValue:UIColorFromRGB(0xA9BD22) forKey:@"_titleTextColor"];
[photoAction setValue:UIColorFromRGB(0xA9BD22) forKey:@"_titleTextColor"];
修改字体,这里我们需要创建一个UILabel的分类
#import
@interface UILabel (SQAlertActionFont)
/** font */
@property (nonatomic,copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
@end
#import "UILabel+SQAlertActionFont.h"
@implementation UILabel (SQAlertActionFont)
- (void)setAppearanceFont:(UIFont *)appearanceFont
{
if(appearanceFont)
{
[self setFont:appearanceFont];
}
}
- (UIFont *)appearanceFont
{
return self.font;
}
@end
使用
UILabel * appearanceLabel = [UILabel appearanceWhenContainedInInstancesOfClasses:@[UIAlertController.class]];(这里注意IOS9.0以下有变化)
UIFont *font = [UIFont systemFontOfSize:14];
[appearanceLabel setAppearanceFont:font];
完整demo在这里:
我的GitHub