UIAlertController custom font, size, color

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

    I'm using it.

    [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
    

    Add one line (AppDelegate) and works for all UIAlertController.

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

    Swift 5.0

    let titleAttrString = NSMutableAttributedString(string: "This is a title", attributes: [NSAttributedString.Key.font: UIFont(name: "CustomFontName", size: 17) as Any])
    let messageAttrString = NSMutableAttributedString(string: "This is a message", attributes: [NSAttributedString.Key.font: UIFont(name: "CustomFontName", size: 13) as Any])
    
    alertController.setValue(titleAttrString, forKey: "attributedTitle")
    alertController.setValue(messageAttrString, forKey: "attributedMessage")
    
    0 讨论(0)
  • 2020-11-22 09:35

    I have created one method objective-C

    -(void)customAlertTitle:(NSString*)title message:(NSString*)message{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)];
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.numberOfLines = 2;
    titleLabel.textColor = [UIColor redColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [subView addSubview:titleLabel];
    
    UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)];
    messageLabel.text = message;
    messageLabel.font = [UIFont systemFontOfSize:18];
    messageLabel.numberOfLines = 2;
    messageLabel.textColor = [UIColor redColor];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    
    [subView addSubview:messageLabel];
    
    [alertView setValue:subView forKey:@"accessoryView"];
    [alertView show];
    }
    

    Code wokring perfectly on Xcode 8.3.1. You can customise according to requirement.

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

    There is a problem with setting the tint color on the view after presenting; even if you do it in the completion block of presentViewController:animated:completion:, it causes a flicker effect on the color of the button titles. This is sloppy, unprofessional and completely unacceptable.

    Other solutions presented depend on the view hierarchy remaining static, something that Apple is loathe to do. Expect those solutions to fail in future releases of iOS.

    The one sure-fire way to solve this problem and to do it everywhere, is via adding a category to UIAlertController and swizzling the viewWillAppear.

    The header:

    //
    //  UIAlertController+iOS9TintFix.h
    //
    //  Created by Flor, Daniel J on 11/2/15.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIAlertController (iOS9TintFix)
    
    + (void)tintFix;
    
    - (void)swizzledViewWillAppear:(BOOL)animated;
    
    @end
    

    The implementation:

    //
    //  UIAlertController+iOS9TintFix.m
    //
    //  Created by Flor, Daniel J on 11/2/15.
    //
    
    #import "UIAlertController+iOS9TintFix.h"
    #import <objc/runtime.h>
    
    @implementation UIAlertController (iOS9TintFix)
    
    + (void)tintFix {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Method method  = class_getInstanceMethod(self, @selector(viewWillAppear:));
            Method swizzle = class_getInstanceMethod(self, @selector(swizzledViewWillAppear:));
            method_exchangeImplementations(method, swizzle);});
    }
    
    - (void)swizzledViewWillAppear:(BOOL)animated {
        [self swizzledViewWillAppear:animated];
        for (UIView *view in self.view.subviews) {
            if (view.tintColor == self.view.tintColor) {
                //only do those that match the main view, so we don't strip the red-tint from destructive buttons.
                self.view.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
                [view setNeedsDisplay];
            }
        }
    }
    
    @end
    

    Add a .pch (precompiled header) to your project and include the category:

    #import "UIAlertController+iOS9TintFix.h"
    

    Make sure you register your pch in the project properly, and it will include the category methods in every class that uses the UIAlertController.

    Then, in your app delegates didFinishLaunchingWithOptions method, import your category and call

    [UIAlertController tintFix];
    

    and it will automatically propagate to every single instance of UIAlertController within your app, whether launched by your code or anyone else's.

    This solution works for both iOS 8.X and iOS 9.X and lacks the flicker of the tint change post-presentation approach. It is also completely agnostic with respect to the view hierarchy of the sub-views of the UIAlertController.

    Happy hacking!

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

    You can change color of button text using this code:

    alertC.view.tintColor = your color;
    

    Maybe this will help you.

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

    Please find this category. I am able to change FONT and Color of UIAlertAction and UIAlertController.

    Use:

    UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
    [appearanceLabel setAppearanceFont:yourDesireFont]];  
    
    0 讨论(0)
提交回复
热议问题