Reputation: 1553
I have created an alert view and I want to align the text of the alert view to left and use this code.
But the problem is that (alert.subviews).count
is 0.
Here is my code:
let alert = UIAlertView(title: "Details:", message: "Composer: abcdfg \nShow:sdfa\nPerformer:asdasdf\nLanguage:farsi\nFirst Line:asdfasdfagadf", delegate: nil, cancelButtonTitle: "OK")
for view in alert.subviews{
if view.isKindOfClass(UILabel){
(view as UILabel).textAlignment = NSTextAlignment.Left
}
}
println((alert.subviews).count)
alert.show()
I want to align the message to left but the subviews count for alert is 0
Upvotes: 6
Views: 9421
Reputation: 5213
let alertMessage = """
You message:
• Point 1
• Point 2
• Point 3
• Point 4
"""
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.left
let attributedMessageText = NSMutableAttributedString(
string: alertMessage,
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0)
]
)
let alertController = UIAlertController.init(title: "Sample title", message: nil, preferredStyle: UIAlertController.Style.alert)
// set attributed message here
alertController.setValue(attributedMessageText, forKey: "attributedMessage")
let actionBlock = UIAlertAction.init(title: "Block", style: UIAlertAction.Style.destructive) { (action) in
self.callWebServiceBlockUserWithUserId(userId: self.arrUserBlocked[sender.tag].id, index: sender.tag)
}
let actionCancel = UIAlertAction.init(title: "Cancel", style: UIAlertAction.Style.cancel) { (action) in
}
alertController.addAction(actionBlock)
alertController.addAction(actionCancel)
self.present(alertController, animated: true, completion: nil)
Upvotes: 4
Reputation: 1
Just make this function, and call it wherever you need
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message:
message, preferredStyle: .alert)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.left
let attributedMessage: NSMutableAttributedString = NSMutableAttributedString(
string: message, // your string message here
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0)
]
)
alertController.setValue(attributedMessage, forKey: "attributedMessage")
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
}))
self.present(alertController, animated: true, completion: nil)
}
Upvotes: 0
Reputation: 4855
Text of alert "Terms and condition" is left Aligned
let alertController = UIAlertController(title: "iosGeek", message: "Terms and Condition", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .cancel) { (action) in
alertController.dismiss(animated: true, completion: nil)
}
alertController.addAction(OKAction)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.left
let messageText = NSMutableAttributedString(
string: "Terms and Condition",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: UIFont.systemFont(ofSize: 13.0)
]
)
alertController.setValue(messageText, forKey: "attributedMessage")
self.present(alertController, animated: true, completion: nil)
Upvotes: 6
Reputation: 539
I found this gist that creates an Alert with left aligned text. To the author of the gist -- thank you very much: https://gist.github.com/nvkiet/29e4d5b5bef0865ee159
Essentially, when creating the UIAlertController:
• provide title
• message
= nil
• create a NSMutableAttributedString in the following way:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.left
let attributedMessage: NSMutableAttributedString = NSMutableAttributedString(
string: message, // your string message here
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: UIFont.systemFont(ofSize: 13.0)
]
)
• Add the attributed string as a key on your alertcontroller:
let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.alert)
alert.setValue(attributedMessage, forKey: "attributedMessage")
• Present the alert
Upvotes: 2
Reputation: 1915
As per the apple documentation you cannot customize the appreance of UIAlertView, please check Appearance of Alert Views section in the given link.
There are two ways i see of resolving this
Upvotes: 0
Reputation: 1325
UIAlertView not support textAlignment
you have to see for custom solution.
https://github.com/wimagguc/ios-custom-alertview
Upvotes: 0