UIAlertController custom font, size, color

后端 未结 25 1688
遥遥无期
遥遥无期 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条回答
  •  旧时难觅i
    2020-11-22 09:56

    Use UIAppearance protocol. Do more hacks with appearanceFont to change font for UIAlertAction.

    Create a category for UILabel

    UILabel+FontAppearance.h

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

    UILabel+FontAppearance.m

    @implementation UILabel (FontAppearance)
    
    - (void)setAppearanceFont:(UIFont *)font
    {
        if (self.tag == 1001) {
            return;
        }
    
        BOOL isBold = (self.font.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold);
        const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
    
        if (self.font.pointSize == 14) {
            // set font for UIAlertController title
            self.font = [UIFont systemFontOfSize:11];
        } else if (self.font.pointSize == 13) {
            // set font for UIAlertController message
            self.font = [UIFont systemFontOfSize:11];
        } else if (isBold) {
            // set font for UIAlertAction with UIAlertActionStyleCancel
            self.font = [UIFont systemFontOfSize:12];
        } else if ((*colors) == 1) {
            // set font for UIAlertAction with UIAlertActionStyleDestructive
            self.font = [UIFont systemFontOfSize:13];
        } else {
            // set font for UIAlertAction with UIAlertActionStyleDefault
            self.font = [UIFont systemFontOfSize:14];
        }
        self.tag = 1001;
    }
    
    - (UIFont *)appearanceFont
    {
        return self.font;
    }
    
    @end
    

    Usage:

    add

    [[UILabel appearanceWhenContainedIn:UIAlertController.class, nil] setAppearanceFont:nil];

    in AppDelegate.m to make it work for all UIAlertController.

提交回复
热议问题