【问题标题】:How to change line spacing of UIAlertController message string?如何更改 UIAlertController 消息字符串的行距?
【发布时间】:2017-09-27 06:47:48
【问题描述】:

我刚刚找到了使用 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)
        })

    }

【问题讨论】:

  • 显示你尝试过的代码
  • 感谢您的关注。我已经编辑了我的问题。

标签: ios swift uikit uialertcontroller


【解决方案1】:

你需要在前面设置段落

let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5

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

【讨论】:

  • 非常感谢。它对我有帮助。你知道如何改变 UIAlertController 的宽度吗?将值设置为 alert.view.frame.size.width 对我不起作用。
  • 根据这篇文章,可以通过在呈现之前进行约束,这是有道理的,stackoverflow.com/questions/25019701/…,但我从来没有这样做过,所以我真的不能说
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多