Làm cách nào để thay đổi phông chữ Tiêu đề của UIAlertController bằng Swift?Swift Cách thay đổi Màu Tiêu đề của UIAlertController
Tôi không nói về màu của tin nhắn, tôi không nói về màu của nút.
Tôi đang nói về Tiêu đề.
Làm cách nào để thay đổi phông chữ Tiêu đề của UIAlertController bằng Swift?Swift Cách thay đổi Màu Tiêu đề của UIAlertController
Tôi không nói về màu của tin nhắn, tôi không nói về màu của nút.
Tôi đang nói về Tiêu đề.
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: .Default) { (action: UIAlertAction!) -> Void in
}
presentViewController(alert,
animated: true,
completion: nil)
Đã thêm dòng mã đúng vào câu trả lời của tôi vì nó ngắn gọn hơn câu trả lời bên dưới.
Điều push25 nói là đúng, chỉ bạn phải sử dụng mã hóa khóa-giá trị để đặt chuỗi được phân bổ. (Cảm ơn 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)
// Điều này sẽ thay đổi màu nút cảnh báoVC.view.tintColor = UIColor.orangeColor() không được hỗ trợ chính xác trong iOS 9 –
@HassanTaleb - Vì tính năng này không hoạt động trong iOS 9, đây là giải pháp để thiết lập ' tintColor' của nút 'UIAlertController': http://stackoverflow.com/a/32636878/4376309 – peacetype
Ngoài ra bạn có thể thêm một UIAlertAction với tiêu đề " "(không gian) và thêm một UILabel tùy chỉnh để UIAlertController.view
tại nơi tiêu đề.
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) {
// ...
}
}
Hoạt động greta. Có ai có thể xác nhận rằng truy cập thuộc tính 'attributedTitle' qua KVO không _ ** không ** _ được tính là sử dụng API riêng tư không? –
Tôi có nghĩa là "tuyệt vời". –
@NicolasMiari Thuộc tính 'attributedTitle' không phải là một phần của API công khai cho [UIAlertController] (https://developer.apple.com/reference/uikit/uialertcontroller) và do đó được coi là riêng tư ([khoản 2.5.1] (https : //developer.apple.com/app-store/review/guidelines/#software-requirements)). Gửi yêu cầu tính năng bằng [Report Reporter] (https://bugreport.apple.com)! – jamesk