Ios 更改UIAlertController的标题颜色
我是iOS开发新手 我正在尝试更改UIAlertController的标题和消息颜色,但不起作用,颜色没有更改 这是我的密码:Ios 更改UIAlertController的标题颜色,ios,swift,xcode,Ios,Swift,Xcode,我是iOS开发新手 我正在尝试更改UIAlertController的标题和消息颜色,但不起作用,颜色没有更改 这是我的密码: let alert = UIAlertController(title: NSLocalizedString("notifications_popup2_title", comment: ""), message: NSLocalizedString("notifications_popup2_message", comment: ""), preferredStyle
let alert = UIAlertController(title: NSLocalizedString("notifications_popup2_title", comment: ""), message: NSLocalizedString("notifications_popup2_message", comment: ""), preferredStyle: UIAlertControllerStyle.actionSheet)
// Change font of the title and message
let titleFont:[NSAttributedStringKey : AnyObject] = [ NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont(name: "Flama-Basic", size: 22)!, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hexString: "#2e2e2e")! ]
let messageFont:[NSAttributedStringKey : AnyObject] = [ NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont(name: "Flama-light", size: 18)!, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hexString: "#2e2e2e")! ]
let attributedTitle = NSMutableAttributedString(string: NSLocalizedString("notifications_popup2_title", comment: ""), attributes: titleFont)
let attributedMessage = NSMutableAttributedString(string: NSLocalizedString("notifications_popup2_message", comment: ""), attributes: messageFont)
alert.setValue(attributedTitle, forKey: "attributedTitle")
alert.setValue(attributedMessage, forKey: "attributedMessage")
如果我将警报样式更改为.alert,则它正在工作
您使用的某些代码已弃用,如:
NSAttributedStringKey
现在是:
NSAttributedString.Key
此外,您不需要指定rawValue。你只需要像这样的东西:
let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: "title", attributes: [.foregroundColor : UIColor.red]), forKey: "attributedTitle")
alertController.setValue(NSAttributedString(string: "message", attributes: [.foregroundColor : UIColor.blue]), forKey: "attributedMessage")
结果:
请注意,它仅适用于.alert。与.alert不同,actionSheet标题和消息不可着色
还要注意,使用私有API可能会导致拒绝。但经验表明,只有名称以开头的私有方法才会导致AppStore私有方法使用检测系统的拒绝
如果苹果决定改变它,它也可以随时停止工作
您最好的选择是自己实施自定义警报。欢迎使用SO!请解释你所说的“但不起作用”是什么意思。还有,我上传了一个截图。我认为没有检测到colorUIAlertController的标题/消息采用字符串属性,而不是属性字符串,因此我;我希望他们不会工作。我猜出于遗留兼容性的原因,它适用于警报。不要。如果使用私有属性/libs,它可能会像在ios12和iOS13之间那样发生变化,因此不再工作,有一天可能会成为苹果拒绝该应用的理由。而是创建您自己的定制的一个。Cf。你完全正确@Sulthan。但经验表明,只有名称以开头的私有方法才会导致AppStore私有方法使用检测系统的拒绝。我在回答中提到了这一点。谢谢,我不是在说拒绝,我是在说这需要一个警告。使用自定义警报控制器通常更安全。很抱歉,我没有理解这一点。你总是对的。我已经更新了答案,并提到了这两个问题。再次感谢@SulthanIt刚刚更改,请参阅IOS13: