如何更改UIAlertController消息字符串的行间距?

时间:2017-04-29 05:49:04

标签: ios swift uikit uialertcontroller

我刚刚找到了使用NSAttributedString在UIAlertController中设置字符串消息的自定义字体,颜色和大小的方法。但是如何在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)
        })

    }

1 个答案:

答案 0 :(得分:2)

您需要在

之前设置段落
let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5

let attributedString = NSAttributedString(
    string: "title",
    attributes: [
        NSParagraphStyleAttributeName: paragraph
    ]
)