gpt4 book ai didi

swift - 使用 addAction 时 UIAlertAction 无法更改文本颜色

转载 作者:行者123 更新时间:2023-11-30 13:42:27 26 4
gpt4 key购买 nike

我的警报框正在使用自定义背景和白色文本,但我似乎无法在我的操作"is"和“否”上获得白色文本(来自 addAction 语句)。当我使用属性字符串作为警报框标题和消息时,白色文本确实显示出用途。

@IBAction func deletePlayer1(sender: UIButton) {
//set player1Name to nil


let alert = UIAlertController(title: "Delete Player", message: "Do you want to delete this player?", preferredStyle: .Alert)

// set font size and color for message in alert box
let attributedString = NSAttributedString(string: "Do you want to delete this player?", attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15),
NSForegroundColorAttributeName : UIColor.whiteColor() ])
alert.setValue(attributedString, forKeyPath: "attributedMessage")

// set font size and color for title of alert box
let attributedString1 = NSAttributedString(string: "Delete Player", attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15),
NSForegroundColorAttributeName : UIColor.whiteColor() ])
alert.setValue(attributedString1, forKeyPath: "attributedTitle")

// set the background color and add rounded labels for the alert box
let subView = alert.view.subviews.first! as UIView
let contentView = subView.subviews.first! as UIView
contentView.backgroundColor = UIColor(red: 64.0/255.0, green: 128.0/255.0, blue: 255.0/255.0, alpha: 1.0)
contentView.layer.cornerRadius = 10


alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

// set font size and color for title of action text in alert box - this is NOT working
let attributedString2 = NSAttributedString(string: "Yes", attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15),
NSForegroundColorAttributeName : UIColor.whiteColor() ])
alert.setValue(attributedString2, forKeyPath: "attributedTitle")

print("you press yes")



}))

alert.addAction(UIAlertAction(title: "No", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

print("you press no")

}))

self.presentViewController(alert, animated: true) { () -> Void in




}

最佳答案

您可以通过将警报的 View tintColor 设置为白色来完成此操作。

alert.view.tintColor = .whiteColor()

关于swift - 使用 addAction 时 UIAlertAction 无法更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35398880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com