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

问题描述 投票:9回答:2

既然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];
ios objective-c ios8 uialertview uiactionsheet
2个回答
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

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

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

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

上面的代码是在颜色和字体上测试的。这只适用于iOS 8或更高版本。


2
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.