Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align Left AlertView Message in iOS8 Swift

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

like image 545
Fatti Khan Avatar asked May 27 '15 12:05

Fatti Khan


1 Answers

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)
like image 196
iOS Geek Avatar answered Oct 16 '22 01:10

iOS Geek