是否可以编辑UIAlertAction标题字体大小和样式?

时间:2014-09-23 06:48:39

标签: ios objective-c ios8 uialertview uiactionsheet

现在,iOS8已弃用UIActionsheetUIAlertview,因此在iOS7上运行的自定义功能不再生效。到目前为止,我所知道的唯一定制是色彩。我需要的是改变标题的字体大小和样式,我没有找到任何方式使用新的UIAlertAction

Already referred to this但我仍然希望有办法改变标题大小和字体。

UIAlertAction

提供一些代码
UIAlertController * alertActionSheetController = [UIAlertController alertControllerWithTitle:@"Settings"
                                                                              message:@""
                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * aboutTheAppAction = [UIAlertAction actionWithTitle:@"About the App"
                                                                 style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction * action){
                                                                   NSLog(@"About the app");
                                                                   [self openAbout];

                                                               }];


[alertActionSheetController addAction:aboutTheAppAction];
[self presentViewController:alertActionSheetController animated:YES completion:nil];

2 个答案:

答案 0 :(得分:13)

您可以更改UIAlertAction的字体和颜色。 首先,您需要添加UILabel类别

@interface UILabel (FontAppearance)
@property (nonatomic, copy) UIFont * appearanceFont UI_APPEARANCE_SELECTOR;
@end

@implementation UILabel (FontAppearance)

-(void)setAppearanceFont:(UIFont *)font {
    if (font)
        [self setFont:font];
}

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

@end

类别文件也在以下网址上载 https://www.dropbox.com/s/em91fh00fv3ut4h/Archive.zip?dl=0

导入该文件后您需要调用以下函数。

UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
[appearanceLabel setAppearanceFont:yourDesireFont]]; 

以上代码在Color和font上进行测试。这只适用于iOS8或更高版本。

答案 1 :(得分:2)

可以使用私有API更改警报操作的字体。它可能会让你被app拒绝,我还没有尝试提交这样的代码。

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

let action = UIAlertAction(title: "Some title", style: .Default, handler: nil)let attributedText = NSMutableAttributedString(string: "Some title")

let range = NSRange(location: 0, length: attributedText.length)
attributedText.addAttribute(NSKernAttributeName, value: 1.5, range: range)
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "ProximaNova-Semibold", size: 20.0)!, range: range)

alert.addAction(action)

presentViewController(alert, animated: true, completion: nil)

// this has to be set after presenting the alert, otherwise the internal property __representer is nil
guard let label = action.valueForKey("__representer")?.valueForKey("label") as? UILabel else { return }
label.attributedText = attributedText