Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController custom font doesn't work at iOS

UIAlertController custom font doesn't work.

The following code is a function ShowActionSheetAsync, show ActionSheet. At this point, I want to change the font of ActionSheet. I have tried several ways, but it didn't work well. Is there good solution?

public Task<bool> ShowActionSheetAsync()
{
    var source = new TaskCompletionSource<bool>();
    var alert = new UIAlertController
    {
        Title = "title"
    };
    alert.AddAction(UIAlertAction.Create(
            "button1",
            UIAlertActionStyle.Default,
            _ => source.SetResult(true)));
    alert.AddAction(UIAlertAction.Create(
        "cancel",
        UIAlertActionStyle.Cancel,
        _ => source.SetResult(false)));

    // [Block 1]
    var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
    ViewController.PresentViewController(alert, true, delegate
    {
        // [Block 2]
    });

    // [Block 3]
    return source.Task;
}

First attempt The following code does not work properly.

  • When I put the code on [Block 1] or [Block 2]

    • It does not work at all
  • When I put the code on [Block 3]

    • Applies only when the first show ActionSheet. From the second times, it does not work

UILabel.AppearanceWhenContainedIn(typeof(UIActionSheet)).Font = UIFont.FromName(StyleResources.MediumFontName, 20);

The second attempt The following code also does not work properly.

  • When I put the code on [Block 2]

    • After showing the default font for a short time, showing the custom font
  • When I put the code on [Block 3]

    • it work on only cancel button

FindDescendantViews<UILabel>() is a extension method for UIView and returns all child view of appropriate type.

var labels = alert.View.FindDescendantViews<UILabel>();
foreach (var label in labels)
{
    label.Font = UIFont.FromName(StyleResources.MediumFontName, 20);
}
like image 626
wplong11 Avatar asked May 24 '16 06:05

wplong11


3 Answers

UIAlertController UILabels font and color can be set via KVC using the attributedTitle key. Use this in [Block 3]

func changeAlert(alert: UIAlertController, backgroundColor: UIColor, textColor: UIColor, buttonColor: UIColor?) {
    let view = alert.view.firstSubview().firstSubview()
    view.backgroundColor = backgroundColor
    view.layer.cornerRadius = 10.0

    // set color to UILabel font
    setSubviewLabelsToTextColor(textColor, view: view)

    // set font to alert via KVC, otherwise it'll get overwritten
    let titleAttributed = NSMutableAttributedString(
        string: alert.title!,
        attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)])
    alert.setValue(titleAttributed, forKey: "attributedTitle")


    let messageAttributed = NSMutableAttributedString(
        string: alert.message!,
        attributes: [NSFontAttributeName:UIFont.systemFontOfSize(13)])
    alert.setValue(messageAttributed, forKey: "attributedMessage")


    // set the buttons to non-blue, if we have buttons
    if let buttonColor = buttonColor {
        alert.view.tintColor = buttonColor
    }
}

func setSubviewLabelsToTextColor(textColor: UIColor, view:UIView) {
    for subview in view.subviews {
        if let label = subview as? UILabel {
            label.textColor = textColor
        } else {
            setSubviewLabelsToTextColor(textColor, view: subview)
        }
    }
}
like image 117
Pau Senabre Avatar answered Nov 05 '22 01:11

Pau Senabre


It's not allowed to either subclass or change default things of an UIAlertController. You need to make a custom view for it.

like image 45
Bhavuk Jain Avatar answered Nov 05 '22 03:11

Bhavuk Jain


I have the solution here. I created a custom AlertThemeConfigurator that recursively runs through all the subviews looking for UILabels, then sets the themed attributedText on them for the title, message, and different types of actions. Feel free to style the attributed strings appropriately.

class AlertThemeConfigurator {

   class func configureAlertViewController(alertController : UIAlertController) {
        AlertLabelConfigurator.adjustLabels(inView: alertController.view, alertController: alertController)
   }

   class AlertLabelConfigurator {

       class func adjustLabels(inView view : UIView, alertController : UIAlertController) {
            for subview in view.subviews {

                if subview is UILabel {
                    adjustLabel((subview as! UILabel), inAlertViewController : alertController)
                }

                adjustLabels(inView :subview,  alertController : alertController)
            }
       }

       class func adjustLabel(label : UILabel, inAlertViewController alertController : UIAlertController) {
            if label.text == alertController.title {
                label.attributedText = attributedTitle(label.text!)
            } else if label.text == alertController.message {
                label.attributedText = attributedBody(label.text!)
            }

            for action in alertController.actions {
                if label.text == action.title {
                    switch action.style {
                    case .Default:
                        label.attributedText = attributedDefaultAction(action.title!)
                    case .Cancel:
                        label.attributedText = attributedCancelAction(action.title!)
                    case .Destructive:
                        label.attributedText = attributedDestructiveAction(action.title!)
                    }
                }
            }
        }

        class func attributedTitle(title : String) -> NSAttributedString {
             let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 20)!, NSForegroundColorAttributeName :  UIColor.greenColor()]
             return NSAttributedString(string: title, attributes: attributes)
        }

        class func attributedBody(title : String) -> NSAttributedString {
             let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 12)!, NSForegroundColorAttributeName :  UIColor.orangeColor()]
             return NSAttributedString(string: title, attributes: attributes)
        }

        class func attributedDefaultAction(title : String) -> NSAttributedString {
             let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName :  UIColor.yellowColor()]
             return NSAttributedString(string: title, attributes: attributes)
        }

        class func attributedCancelAction(title : String) -> NSAttributedString {
             let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName :  UIColor.purpleColor()]
             return NSAttributedString(string: title, attributes: attributes)
        }

        class func attributedDestructiveAction(title : String) -> NSAttributedString {
             let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName :  UIColor.redColor()]
             return NSAttributedString(string: title, attributes: attributes)
        }
    }
}

To present it call

 let alert = CustomAlertController(title: "Title", message:"Message" , preferredStyle: UIAlertControllerStyle.ActionSheet)
 alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
 presentViewController(alert, animated: true, completion: nil)
 AlertThemeConfigurator.configureAlertViewController(alert)
like image 29
AntonTheDev Avatar answered Nov 05 '22 03:11

AntonTheDev