简体   繁体   中英

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

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)

在此处输入图片说明

swift 5 Sample code

    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)

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

UIAlertView not support textAlignment

you have to see for custom solution.

https://github.com/wimagguc/ios-custom-alertview

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

  1. Use any third party component which suits your requirement.
  2. Ask your designer to present the information in some other manner for which you were using the alert view.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM