【问题标题】:Swift How to change UIAlertController's Title ColorSwift 如何更改 UIAlertController 标题颜色
【发布时间】:2015-10-18 04:47:02
【问题描述】:

如何使用 Swift 更改 UIAlertController 的 Title 字体?

我不是在谈论消息颜色,我不是在谈论按钮颜色。

我说的是标题。

【问题讨论】:

    标签: ios xcode swift user-interface


    【解决方案1】:
    let attributedString = NSAttributedString(string: "Title", attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here
        NSForegroundColorAttributeName : UIColor.redColor()
    ])
    
    let alert = UIAlertController(title: "", message: "",  preferredStyle: .alert)
    
    alert.setValue(attributedString, forKey: "attributedTitle")
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
    }
    
    alert.addAction(cancelAction)
    
    present(alert, animated: true, completion: nil)
    

    在我的答案中添加了正确的代码行,因为它比下面的答案更简洁。

    【讨论】:

    • 作品 greta。谁能确认通过 KVO 访问属性 attributedTitle 是否算作使用私有 API?
    • @NicolasMiari attributedTitle 属性不是 UIAlertController 的公共 API 的一部分,因此被视为私有 (clause 2.5.1)。使用Bug Reporter! 提交功能请求!
    • @jamesk 你没看错,但是这个答案的支持数表明人们在使用它时没有任何问题。
    • @pbush25 你可能是对的。另一种可能性是,在开发过程中,开发人员会投票赞成似乎有效的答案,然后当应用程序被拒绝时,开发人员会沮丧地咬牙切齿,但不会费心回去对他们赞成的所有答案投反对票。谁能说?公平地说,使用像attributedTitle 这样通用的密钥看起来确实无害,所以它至少有这么多的用处。
    • 我确实在 Apple 论坛上看到其他开发人员在使用自定义字体时遇到截断问题(因为显然所有大小都是使用系统字体完成的)。因此,考虑到这个问题可能会出现,而 Apple 并未正式支持这一点,我对这样做持谨慎态度。
    【解决方案2】:
    Alert(self, Title: “Hello”, TitleColor: UIColor.whiteColor(), Message: “World”, MessageColor: UIColor.whiteColor(), BackgroundColor: UIColor.blackColor(), BorderColor: UIColor.yellowColor(), ButtonColor: UIColor.yellowColor())
        func Alert(View: ViewController, Title: String, TitleColor: UIColor, Message: String, MessageColor: UIColor, BackgroundColor: UIColor, BorderColor: UIColor, ButtonColor: UIColor) {
    
        let TitleString = NSAttributedString(string: Title, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : TitleColor])
        let MessageString = NSAttributedString(string: Message, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : MessageColor])
    
        let alertController = UIAlertController(title: Title, message: Message, preferredStyle: .Alert)
    
        alertController.setValue(TitleString, forKey: "attributedTitle")
        alertController.setValue(MessageString, forKey: "attributedMessage")
    
        let okAction = UIAlertAction(title: "OK", style: .Default) { (action) in
    
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
    
    
        let subview = alertController.view.subviews.first! as UIView
        let alertContentView = subview.subviews.first! as UIView
        alertContentView.backgroundColor = BackgroundColor
        alertContentView.layer.cornerRadius = 10
        alertContentView.alpha = 1
        alertContentView.layer.borderWidth = 1
        alertContentView.layer.borderColor = BorderColor.CGColor
    
    
        //alertContentView.tintColor = UIColor.whiteColor()
        alertController.view.tintColor = ButtonColor
    
        View.presentViewController(alertController, animated: true) {
            // ...
        }
    }
    

    【讨论】:

    • 太棒了!如何更改操作按钮的分隔线颜色?
    • 我不相信这是可能的,但我可能是错的,或者您必须创建自己的自定义警报框。
    【解决方案3】:

    push25 说的没错,只是你必须使用键值编码来设置属性字符串。 (感谢dupuis2387

        //Define a color
        let color = UIColor.redColor()
    
        //Make a controller
        let alertVC = UIAlertController(title: "Dont care what goes here, since we're about to change below", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    
        //Title String
        var hogan = NSMutableAttributedString(string: "Presenting the great... Hulk Hogan!")
    
        //Make the attributes, like size and color
        hogan.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(40.0), range: NSMakeRange(24, 11))
    
        hogan.addAttribute(NSForegroundColorAttributeName, value: color, range: NSMakeRange(0, NSString(string: hogan.string).length))
    
        //Set the new title
        //Use "attributedMessage" for the message
        alertVC.setValue(hogan, forKey: "attributedTitle")
    
        //This will change the button color
        alertVC.view.tintColor = UIColor.orangeColor()
    
        //Make the button
        let button:UIAlertAction  = UIAlertAction(title: "Label text", style: UIAlertActionStyle.Default, handler: { (e:UIAlertAction!) -> Void in
            println("\(e)")
        })
    
        //You can add images to the button
        let accessoryImage:UIImage = UIImage(named: "someImage")!
        button.setValue(accessoryImage, forKey:"image")
    
        //Add the button to the alert
        alertVC.addAction(button)
    
        //Finally present it
        self.presentViewController(alertVC, animated: true, completion:  nil)
    

    【讨论】:

    • //这会改变按钮颜色 alertVC.view.tintColor = UIColor.orangeColor() is not supported in iOS 9
    • @HassanTaleb - 因为这在 iOS 9 中不起作用,所以这里是设置 UIAlertController 按钮的 tintColor 的解决方法:stackoverflow.com/a/32636878/4376309
    【解决方案4】:

    我构造了这个类:

    class Alert {
    class func showAlert(title: String, titleColor: UIColor, message: String, preferredStyle: UIAlertControllerStyle, titleAction: String, actionStyle: UIAlertActionStyle, vc: UIViewController) {
    
        let attributedString = NSAttributedString(string: title, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: titleColor])
        let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
    
        alert.setValue(attributedString, forKey: "attributedTitle")
    
        alert.addAction(UIAlertAction(title: titleAction, style: actionStyle, handler: nil))
        vc.present(alert, animated: true)
    }
    

    }

    用法

    Alert.showAlert(title: "yourTitle", titleColor: .yourcolor, message: "your message", preferredStyle: .alert, titleAction: "your title action", actionStyle: .yourActionStyle, vc: self)
    

    希望有帮助:)

    【讨论】:

      【解决方案5】:

      这是 Swift 4 ++ 也使用 UIAlert 的全局实例

      func showAlertError(withTitle title: String, withMessage message: String) {
          var dialogTitle = title
          if dialogTitle.isEmpty { dialogTitle = "Error:" }
      
      
          let attributedString = NSAttributedString(string: dialogTitle, attributes: [
              NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15),
              NSAttributedString.Key.foregroundColor : UIColor.red
              ])
      
          let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
          alert.setValue(attributedString, forKey: "attributedTitle")
          let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
      
          alert.addAction(ok)
          DispatchQueue.main.async(execute: {
              self.present(alert, animated: true)
          })
      }
      

      【讨论】:

        【解决方案6】:

        您还可以添加标题为“”(空格)的 UIAlertAction,并在标题位置向 UIAlertController.view 添加自定义 UILabel。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多