UIAlertController更改字体颜色

时间:2015-01-12 05:34:12

标签: swift ios8 uialertcontroller

这是我创建UIAlertController的代码

    // Create the alert controller
    var alertController = UIAlertController(title: "Are you sure you want to call \(self.number)?", message: "", preferredStyle: .Alert)

    // Create the actions
    var okAction = UIAlertAction(title: "Call", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        var url:NSURL = NSURL(string: "tel://\(self.number)")!
        UIApplication.sharedApplication().openURL(url)
    }

    var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
        UIAlertAction in

    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.presentViewController(alertController, animated: true, completion: nil)

我无法弄清楚如何更改取消和调用操作的文本颜色。标题文本目前为黑色,取消和呼叫按钮为白色。我正在努力让它们全黑,以提高能见度。有任何想法吗?谢谢!

8 个答案:

答案 0 :(得分:17)

经过一些反复试验,我发现这很有效。希望这将有助于未来迅速的新人!

alertController.view.tintColor = UIColor.blackColor()

答案 1 :(得分:5)

以下代码正在更改UIAlertView标题颜色。

  let alert = UIAlertController(title: messageTitle, message: messageText, preferredStyle: UIAlertControllerStyle.Alert)

  alert.setValue(NSAttributedString(string: messageTitle, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17),NSForegroundColorAttributeName : UIColor.redColor()]), forKey: "attributedTitle")

  alert.addAction(UIAlertAction(title: buttonText, style: UIAlertActionStyle.Default, handler: nil))

  parent.presentViewController(alert, animated: true, completion: nil)

如果您想更改按钮颜色,请在当前的View Controller之后添加以下代码。

  alert.view.tintColor = UIColor.redColor()

答案 2 :(得分:5)

Swift 4.2

执行此操作的一种方法是在UIAlertController上进行扩展,使您的所有应用程序警报都具有相同的颜色。但这会留下红色的破坏作用。

 extension UIAlertController{
        open override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
           self.view.tintColor = .yourcolor
        }
    }

答案 3 :(得分:3)

Piyush的回答对我有所帮助,但这里有一些Swift 3的调整,并分别更改标题和消息。

名称:

alert.setValue(NSAttributedString(string: alert.message, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 29, weight: UIFontWeightMedium), NSForegroundColorAttributeName : UIColor.red]), forKey: "attributedTitle")

消息:

alert.setValue(NSAttributedString(string: alert.message, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 29, weight: UIFontWeightMedium), NSForegroundColorAttributeName : UIColor.red]), forKey: "attributedMessage")

大字体是因为我实际上需要为tvOS做这件事,在它和iOS上都很棒。

答案 4 :(得分:1)

Swift 5 XCode 11.1 及更高版本中,警报标题的颜色

alert.setValue(NSAttributedString(string: alert.title!, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 25, weight: UIFont.Weight.medium), NSAttributedString.Key.foregroundColor : UIColor.red]), forKey: "attributedTitle")

警报消息的颜色:

alert.setValue(NSAttributedString(string: alert.message!, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20,weight: UIFont.Weight.medium),NSAttributedString.Key.foregroundColor :UIColor.black]), forKey: "attributedMessage")

答案 5 :(得分:0)

在iOS 9.0之前,您只需更改基础视图的tintColor,如下所示:

alertController.view.tintColor = UIColor.redColor()

但是,由于iOS 9中引入了一个错误,您可以:

  1. 在AppDelegate中更改app tintColor。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        self.window.tintColor = UIColor.redColor()
        return true
    }
    
  2. 在完成块中重新应用颜色。

    self.presentViewController(alert, animated: true, completion: {() -> Void in
        alert.tintColor = UIColor.redColor()
    })
    
  3. 请在此处查看我的其他答案:https://stackoverflow.com/a/37737212/1781087

答案 6 :(得分:0)

我遇到了同样的问题,花了很多时间试图找到改变iOS 9和iOS 10 +颜色的最佳方法,因为它以不同的方式实现。

最后,我为UIViewController做了一个扩展。在扩展中,我添加了自定义函数,它几乎等于默认函数“present”,但执行颜色的修复。在这里你是我的解决方案。适用于swift 3+,适用于从iOS 9开始的目标项目:

extension UIViewController {
    /// Function for presenting AlertViewController with fixed colors for iOS 9
    func presentAlert(alert: UIAlertController, animated flag: Bool, completion: (() -> Swift.Void)? = nil){
        // Temporary change global colors
        UIView.appearance().tintColor = UIColor.red // Set here whatever color you want for text
        UIApplication.shared.keyWindow?.tintColor = UIColor.red // Set here whatever color you want for text

        //Present the controller
        self.present(alert, animated: flag, completion: {
            // Rollback change global colors
            UIView.appearance().tintColor = UIColor.black // Set here your default color for your application.
            UIApplication.shared.keyWindow?.tintColor = UIColor.black // Set here your default color for your application.
            if completion != nil {
                completion!()
            }
        })
    }
}

要使用此固定功能,您应该只调用此函数而不是默认的当前函数。例如:

self.presentAlert(alert: alert, animated: true)

相同的解决方案,但对于UIActivityViewController:

extension UIViewController {
    /// Function for presenting UIActivityViewController with fixed colors for iOS 9 and 10+
    func presentActivityVC(vc: UIActivityViewController, animated flag: Bool, completion: (() -> Swift.Void)? = nil) {
        // Temporary change global colors for changing "Cancel" button color for iOS 9 and 10+
        if UIDevice.current.systemVersion.range(of: "9.") != nil {
            UIApplication.shared.keyWindow?.tintColor = ColorThemes.alertViewButtonTextColor
        } else {
            UILabel.appearance().textColor = ColorThemes.alertViewButtonTextColor
        }

        self.present(vc, animated: flag) {
            // Rollback for changing global colors for changing "Cancel" button color for iOS 9 and 10+
            if UIDevice.current.systemVersion.range(of: "9.") != nil {
                UIApplication.shared.keyWindow?.tintColor = ColorThemes.tintColor
            } else {
                UILabel.appearance().textColor = ColorThemes.textColorNormal
            }
            if completion != nil {
                completion!()
            }
        }
    }
}

我希望这会对某人有所帮助,并且会节省很多时间。因为我的时间没有被这样详细的答案所保存:)

答案 7 :(得分:0)

这是Swift 4的更新,以Cody的答案为基础:

设置警报标题的颜色:

from django.urls import path

from . import views

urlpatterns = [
    path('<int:index>',views.detail, name='art_rec'),
]

设置警报消息的颜色:

alert.setValue(NSAttributedString(string: alert.title!, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.medium), NSAttributedStringKey.foregroundColor : UIColor.blue]), forKey: "attributedTitle")

按照https://developer.apple.com/documentation/foundation/nsattributedstring/key