UIAlertController custom font, size, color

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

    You can change the button color by applying a tint color to an UIAlertController.

    On iOS 9, if the window tint color was set to a custom color, you have to apply the tint color right after presenting the alert. Otherwise the tint color will be reset to your custom window tint color.

    // In your AppDelegate for example:
    window?.tintColor = UIColor.redColor()
    
    // Elsewhere in the App:
    let alertVC = UIAlertController(title: "Title", message: "message", preferredStyle: .Alert)
    alertVC.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    alertVC.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
    
    // Works on iOS 8, but not on iOS 9
    // On iOS 9 the button color will be red
    alertVC.view.tintColor = UIColor.greenColor()
    
    self.presentViewController(alert, animated: true, completion: nil)
    
    // Necessary to apply tint on iOS 9
    alertVC.view.tintColor = UIColor.greenColor()
    
    0 讨论(0)
  • 2020-11-22 09:44

    Swift 4

    Example of custom font on the title. Same things for other components such as message or actions.

        let titleAttributed = NSMutableAttributedString(
                string: Constant.Strings.cancelAbsence, 
                attributes: [NSAttributedStringKey.font:UIFont(name:"FONT_NAME",size: FONT_SIZE)]
        )
    
        let alertController = UIAlertController(
            title: "",
            message: "",
            preferredStyle: UIAlertControllerStyle.YOUR_STYLE
        )
    
        alertController.setValue(titleAttributed, forKey : "attributedTitle")
        present(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
  • 2020-11-22 09:46

    I just use this kind of demand, seemingly and system, details are slightly different, so we are ... OC realized Alert and Sheet popup window encapsulation.

    Often encountered in daily development need to add a figure to Alert or change a button color, such as "simple" demand, today brings a and system components of highly similar and can fully meet the demand of customized packaging components.

    Github:https://github.com/ReverseScale/RSCustomAlertView

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

    I work for Urban Outfitters. We have an open source pod, URBNAlert, that we used in all of our apps. It's based off of UIAlertController, but is highly customizable.

    Source is here: https://github.com/urbn/URBNAlert

    Or simply install by the pod by placing URBNAlert in your Podfile

    Heres some sample code:

    URBNAlertViewController *uac = [[URBNAlertViewController alloc] initWithTitle:@"The Title of my message can be up to 2 lines long. It wraps and centers." message:@"And the message that is a bunch of text. And the message that is a bunch of text. And the message that is a bunch of text."];
    
    // You can customize style elements per alert as well. These will override the global style just for this alert.
    uac.alertStyler.blurTintColor = [[UIColor orangeColor] colorWithAlphaComponent:0.4];
    uac.alertStyler.backgroundColor = [UIColor orangeColor];
    uac.alertStyler.textFieldEdgeInsets = UIEdgeInsetsMake(0.0, 15.0, 0.0, 15.0);
    uac.alertStyler.titleColor = [UIColor purpleColor];
    uac.alertStyler.titleFont = [UIFont fontWithName:@"Chalkduster" size:30];
    uac.alertStyler.messageColor = [UIColor blackColor];
    uac.alertStyler.alertMinWidth = @150;
    uac.alertStyler.alertMaxWidth = @200;
    // many more styling options available 
    
    [uac addAction:[URBNAlertAction actionWithTitle:@"Ok" actionType:URBNAlertActionTypeNormal actionCompleted:^(URBNAlertAction *action) {
          // Do something
    }]];
    
    [uac addAction:[URBNAlertAction actionWithTitle:@"Cancel" actionType:URBNAlertActionTypeCancel actionCompleted:^(URBNAlertAction *action) {
          // Do something
    }]];
    
    [uac show];
    
    0 讨论(0)
  • 2020-11-22 09:49

    Not sure if this is against private APIs/properties but using KVC works for me on ios8

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
    [hogan addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:50.0]
                  range:NSMakeRange(24, 11)];
    [alertVC setValue:hogan forKey:@"attributedTitle"];
    
    
    
    UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text" 
                                            style:UIAlertActionStyleDefault
                                            handler:^(UIAlertAction *action){
                                                        //add code to make something happen once tapped
    }];
    UIImage *accessoryImage = [UIImage imageNamed:@"someImage"];
    [button setValue:accessoryImage forKey:@"image"];
    

    For the record, it is possible to change alert action's font as well, using those private APIs. Again, it may get you app rejected, I have not yet tried to submit such code.

    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
    

    For Swift 4.2 in XCode 10 and up the last 2 lines are now:

    guard let label = (action!.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return }
            label.attributedText = attributedText
    
    0 讨论(0)
提交回复
热议问题