UIAlertController custom font, size, color

后端 未结 25 1644
遥遥无期
遥遥无期 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:54

    A bit clunky, but this works for me right now to set background and text colors. I found it here.

    UIView * firstView = alertController.view.subviews.firstObject;
        UIView * nextView = firstView.subviews.firstObject;
        nextView.backgroundColor = [UIColor blackColor];
    
    0 讨论(0)
  • 2020-11-22 09:55

    Solution/Hack for iOS9

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Test Error" message:@"This is a test" preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"Alert View Displayed");
     [[[[UIApplication sharedApplication] delegate] window] setTintColor:[UIColor whiteColor]];
        }];
    
        [alertController addAction:cancelAction];
        [[[[UIApplication sharedApplication] delegate] window] setTintColor:[UIColor blackColor]];
        [self presentViewController:alertController animated:YES completion:^{
            NSLog(@"View Controller Displayed");
        }];
    
    0 讨论(0)
  • 2020-11-22 09:56

    A Swift translation of the @dupuis2387 answer. Worked out the syntax to set the UIAlertController title's color and font via KVC using the attributedTitle key.

    let message = "Some message goes here."
    let alertController = UIAlertController(
        title: "", // This gets overridden below.
        message: message,
        preferredStyle: .Alert
    )
    let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ -> Void in
    }
    alertController.addAction(okAction)
    
    let fontAwesomeHeart = "\u{f004}"
    let fontAwesomeFont = UIFont(name: "FontAwesome", size: 17)!
    let customTitle:NSString = "I \(fontAwesomeHeart) Swift" // Use NSString, which lets you call rangeOfString()
    let systemBoldAttributes:[String : AnyObject] = [ 
        // setting the attributed title wipes out the default bold font,
        // so we need to reconstruct it.
        NSFontAttributeName : UIFont.boldSystemFontOfSize(17)
    ]
    let attributedString = NSMutableAttributedString(string: customTitle as String, attributes:systemBoldAttributes)
    let fontAwesomeAttributes = [
        NSFontAttributeName: fontAwesomeFont,
        NSForegroundColorAttributeName : UIColor.redColor()
    ]
    let matchRange = customTitle.rangeOfString(fontAwesomeHeart)
    attributedString.addAttributes(fontAwesomeAttributes, range: matchRange)
    alertController.setValue(attributedString, forKey: "attributedTitle")
    
    self.presentViewController(alertController, animated: true, completion: nil)
    

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 09:57

    For iOS 9.0 and above use this code in app delegate

    [[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor redColor]];
    
    0 讨论(0)
  • 2020-11-22 09:58

    To change the color of one button like CANCEL to the red color you can use this style property called UIAlertActionStyle.destructive :

    let prompt = UIAlertController.init(title: "Reset Password", message: "Enter Your E-mail :", preferredStyle: .alert)
            let okAction = UIAlertAction.init(title: "Submit", style: .default) { (action) in
                  //your code
    }
    
    let cancelAction = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.destructive) { (action) in
                    //your code
            }
            prompt.addTextField(configurationHandler: nil)
            prompt.addAction(okAction)
            prompt.addAction(cancelAction)
            present(prompt, animated: true, completion: nil);
    
    0 讨论(0)
提交回复
热议问题