UIAlertController custom font, size, color

后端 未结 25 2148
遥遥无期
遥遥无期 2020-11-22 09:06

I am using new UIAlertController for showing alerts. I have this code:

// nil titles break alert interface on iOS 8.0, so we\'ll be using empty strings
UIAle         


        
25条回答
  •  梦谈多话
    2020-11-22 09:46

    Use UIAppearance protocol. Example for setting a font - create a category to extend 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
    

    And its usage:

    UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
    [appearanceLabel setAppearanceFont:[UIFont boldSystemFontOfSize:10]]; //for example
    

    Tested and working with style UIAlertControllerStyleActionSheet, but I guess it will work with UIAlertControllerStyleAlert too.

    P.S. Better check for class availability instead of iOS version:

    if ([UIAlertController class]) {
        // UIAlertController code (iOS 8)
    } else {
        // UIAlertView code (pre iOS 8)
    }
    

提交回复
热议问题