如何更改UIAlertViewController标题颜色?

问题描述 投票:0回答:1

请参见下面的代码。我正在尝试在Swift 5中设置UIAlertViewController的颜色和字体。

除了标题颜色以外,其他都不错。

我在SO中尝试了各种答案,但无法获得白色标题的结果

//设为控制器让alertController = UIAlertController(title:“”,message:“”,preferredStyle:.actionSheet)

        // Change background and tintColor of UIAlertController
        alertController.setBackgroundColor(color: .darkGray)
        alertController.setTint(color: .yellow)

        //Title String
        let titleString = NSMutableAttributedString(string: "Project Details or See items?".uppercased())

        titleString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: NSMakeRange(0, titleString.length))
        titleString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20.0), range: NSMakeRange(0, titleString.length))

      alertController.setValue(titleString, forKey: "attributedTitle")



        alertController.addAction(UIAlertAction(title: "Project Details", style: .default, handler: { _ in

            self.performSegue(withIdentifier: "ProjectToDetails", sender: self)
        }))

        alertController.addAction(UIAlertAction(title: "See Items", style: .default, handler: { _ in

            self.performSegue(withIdentifier: "ProjectToItems", sender: self)
        }))

        alertController.addAction(UIAlertAction.init(title: "Cancel", style: .destructive, handler: nil))


        if let popoverController = alertController.popoverPresentationController {
            popoverController.sourceView = self.tableView
            popoverController.sourceRect = CGRect(x: rectOfCellInTableView.midX + 50, y: rectOfCellInTableView.midY, width: 0, height: 0)
            popoverController.permittedArrowDirections = .left
        }

        self.present(alertController, animated: true, completion: nil)

请参阅附件的屏幕截图,其中显示了正在运行的AlertController

enter image description here

swift nsattributedstring uialertcontroller
1个回答
0
投票

您可以使用UIAlertController extension,因此该代码可在整个项目中重复使用。

extension UIAlertController {

    //Set background color of UIAlertController
    func setBackgroundColor(color: UIColor) {
        if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
            contentView.backgroundColor = color
        }
    }

    //Set title font and title color
    func setTitlet(font: UIFont?, color: UIColor?) {
        guard let title = self.title else { return }
        let attributeString = NSMutableAttributedString(string: title)//1
        if let titleFont = font {
            attributeString.addAttributes([NSAttributedString.Key.font : titleFont],//2
                                          range: NSMakeRange(0, title.utf8.count))
        }

        if let titleColor = color {
            attributeString.addAttributes([NSAttributedString.Key.foregroundColor : titleColor],//3
                                          range: NSMakeRange(0, title.utf8.count))
        }
        self.setValue(attributeString, forKey: "attributedTitle")//4
    }

    //Set tint color of UIAlertController
    func setTint(color: UIColor) {
        self.view.tintColor = color
    }

}

现在在ViewController中,在viewDidAppear函数中添加以下代码。

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let alertController = UIAlertController(title: "Alert!!", message: "This is alert message", preferredStyle: .alert)

        // Change font and color of title
        alertController.setTitlet(font: UIFont.boldSystemFont(ofSize: 26), color: .white)

        // Change background color of UIAlertController
        alertController.setBackgroundColor(color: .darkGray)

        // Change tint color of UIAlertController
        alertController.setTint(color: .yellow)

        // .........
    }

希望它对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.