How to edit UIAlertAction text font size and color

后端 未结 7 881
时光取名叫无心
时光取名叫无心 2020-12-06 01:33

How to edit UIAlertAction text size and color? I have taken a UIAlertController acoording to it how to edit the size. This i smy Code



        
相关标签:
7条回答
  • 2020-12-06 01:39

    Use NSMutableAttributedString set the font size and color, use this below code,

    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Do you wish to logout?"];
    
    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)];
    
    [hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,35)];
    
    [controller setValue:hogan forKey:@"attributedTitle"];
    
    UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
    

    hope its helpful

    0 讨论(0)
  • 2020-12-06 01:41

    I've written an extension

    extension UIAlertController{
        open override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
    
            for i in self.actions {
                let attributedText = NSAttributedString(string: i.title ?? "", attributes: [NSAttributedString.Key.font : UIFont(name: "SFProText-Semibold", size: 20.0)!])
    
                guard let label = (i.value(forKey: "__representer") as AnyObject).value(forKey: "label") as? UILabel else { return }
                label.attributedText = attributedText
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-06 01:42

    Try this:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"abcd" preferredStyle:UIAlertControllerStyleActionSheet];
    
    NSMutableAttributedString *xyz = [[NSMutableAttributedString alloc] initWithString:@"pqrs"];
    
    [xyz addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:30.0]
                  range:NSMakeRange(20, 15)];
    [alert setValue:xyz forKey:@"attributedTitle"];
    
    
    
    UIAlertAction *logout = [UIAlertAction actionWithTitle:@"logout" 
                                            style:UIAlertActionStyleDefault
                                            handler:^(UIAlertAction *action){
                                                        //your code for handling this
    }];
    UIImage *Image = [UIImage imageNamed:@"yourImage"];
    [logout setValue:accessoryImage forKey:@"image"];
    
    0 讨论(0)
  • 2020-12-06 01:50

    Changing the color is pretty simple.

    You could just change the tintColor of the underlying view, however, due to a known bug introduced in iOS 9 (https://openradar.appspot.com/22209332), the tintColor is overridden by the application window's tintColor.

    See my full answer to: How to change tint color of UIAlertController


    You shouldn't change the UIAlertController font. However it still can be done, see this answer

    0 讨论(0)
  • 2020-12-06 01:54

    You can update text color using

           UIAlertAction *myGoalAction = [UIAlertAction
                                        actionWithTitle:NSLocalizedString(@"My Title", @"My Title")
                                        style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action)
                                        {
    
                                        }];
           [myGoalAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
    

    There is no efficient way to update font size.I will suggest you to use standard font size.

    UIAlertAction title label is private variable and not accessible directly. Label comes inside 3 level private view hierarchy. Showing logout action with bigger font make sense for app.

    There are many open source solution available.I will recommend to try this

    0 讨论(0)
  • 2020-12-06 01:59

    Here is giving my answer Swift 5. We can apply custom fonts to AlertView and AlertAddAction.
    1st Create AlertView Extention. Inside Extension

    import Foundation
    import UIKit
    
    extension UIAlertController {
        
        func applyBranding() {
            
            applyAlertTitleBranding()
            applyAlertMessageBranding()
        }
        
        func applyAlertTitleBranding() {
            let titleFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 18.0)!]
            let titleAttrString = NSMutableAttributedString(string: title!, attributes: titleFont as [NSAttributedString.Key : Any])
            let titleColor = UIColor(red: 34.0/255/0, green: 34.0/255/0, blue: 34.0/255/0, alpha: 1.0)
            titleAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                         value: titleColor,
                                         range: NSRange(location: 0, length: title!.count))
            setValue(titleAttrString, forKey: "attributedTitle")
        }
        
        func applyAlertMessageBranding() {
            let messageFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 14.0)!]
            let messageAttrString = NSMutableAttributedString(string: message!, attributes: messageFont as [NSAttributedString.Key : Any])
            let messageTitleColor = UIColor(red: 68.0/255/0, green: 68.0/255/0, blue: 68.0/255/0, alpha: 1.0)
            messageAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                           value: messageTitleColor,
                                           range: NSRange(location: 0, length: message!.count))
            setValue(messageAttrString, forKey: "attributedMessage")
        }
        
        func applyNoActionBranding() {
            let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 16.0)!]
            for actionButton in actions {
                let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
                actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
            }
        }
        
        func applyYesActionBranding() {
            let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 16.0)!]
            for actionButton in actions {
                let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
                actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
            }
        }
    }
    
    
    
     
    

    2nd call those AlertView functions where ever you need. For example call in ViewDidLoad()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        showAlert()
    }
    
    func showAlert() {
    
        let alertVc = UIAlertController(title: "Some Title", message: "Some message", preferredStyle: .alert)
        
        //No action
        let noAction = UIAlertAction(title: "No", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            alertVc.dismiss(animated: true, completion: nil)
        })
        
        //Yes action
        let yesAction = UIAlertAction(title: "Yes", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            self.deleteFunction(address)
            alertVc.dismiss(animated: true, completion: nil)
        })
        
        alertVc.applyBranding()
        alertVc.applyNoActionBranding()
        alertVc.applyYesActionBranding()
        alertVc.addAction(noAction)
        alertVc.addAction(yesAction)
        
        alertVc.view.tintColor = .blue
        DispatchQueue.main.async(execute: {
            self.present(alertVc, animated: true, completion: nil)
        })
    }
    

    Hope It's work for all

    0 讨论(0)
提交回复
热议问题