0

I just found ways to set the custom font,color and size of string message inside UIAlertController using NSAttributedString.But how can I set line spacing property of that string in swift?

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let alert = UIAlertController(title: "", message: "",  preferredStyle: .alert)
         var known_String = ""
         var actionDone_String = ""
        if (self.reports_array[indexPath.row].is_know == 1 ){
            known_String = "We have known the problem"
            let attributedString = NSAttributedString(string: known_String, attributes: [
                NSFontAttributeName : UIFont.systemFont(ofSize: 18), //your font here
                NSForegroundColorAttributeName : UIColor.black
                ])
            alert.setValue(attributedString, forKey: "attributedTitle")

        }
        if (self.reports_array[indexPath.row].is_solved == 1 ){
            actionDone_String = "\n\(self.reports_array[indexPath.row].report_reply)"
            let attributedMessage = NSAttributedString(string: actionDone_String, attributes: [
                NSFontAttributeName : UIFont.systemFont(ofSize: 17), //your font here
                NSForegroundColorAttributeName : UIColor().HexToColor(hexString: "32469A", alpha: 1.0),
                NSParagraphStyleAttributeName:NSMutableParagraphStyle(),
                ])
               alert.setValue(attributedMessage, forKey: "attributedMessage")
        }

        let cancelAction = UIAlertAction(title: "Ok",
                                         style: .default) { (action: UIAlertAction!) -> Void in
        }

        alert.addAction(cancelAction)


        DispatchQueue.main.async(execute: {
            self.present(alert,animated: true,completion: nil)
        })

    }
Min Htet Oo
  • 188
  • 4
  • 16

1 Answers1

2

You need to set paragraph before

let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5

let attributedString = NSAttributedString(
    string: "title",
    attributes: [
        NSParagraphStyleAttributeName: paragraph
    ]
)
Luzo
  • 1,336
  • 10
  • 15
  • Thanks a lot.It helps me.Do you known how to change the width of UIAlertController? Setting value to alert.view.frame.size.width doesn't work for me. – Min Htet Oo Apr 29 '17 at 06:20
  • According to this post it is possible by constraining before presenting, which makes sense, http://stackoverflow.com/questions/25019701/how-to-change-uialertcontroller-height , but I never had to do this so I can't really say – Luzo Apr 29 '17 at 07:05