ios - Swift 3 中 UIAlertController 中的自动换行

标签 ios swift3 uialertcontroller

我正在使用 swift 3 构建一个测验应用程序,它利用 UITableViewController 列出问题,我想使用的是一个警报 View 来发布问题,并提供多种答案供您选择。我让它以我想要的方式工作,但有一个巨大的怪癖,那就是长答案的字体要小得多,并且通过用省略号替换答案中间的文本来截断。

如何在 AlertView 操作中获得自动换行的答案,并保持相同大小的字体?请注意,在一定字符长度后手动在答案中插入“\n”是不现实的,因为问题词典中有数百个问题。

enter image description here

这是 AlertView 的代码:

func showQuestion(questionNum: Int) {

        let alertTitle = "Question \(questionNum + 1)"
        let qDict = test[questionNum] as! [String:String]
        let alertMessage = qDict["Question"]

        // create the alert
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)

        // add the actions (buttons)
        if qDict["Option-A"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-A"], style: UIAlertActionStyle.default, handler: nil))
        }

        if qDict["Option-B"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-B"], style: UIAlertActionStyle.default, handler: nil))
        }

        if qDict["Option-C"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-C"], style: UIAlertActionStyle.default, handler: nil))
        }

        if qDict["Option-D"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-D"], style: UIAlertActionStyle.default, handler: nil))
        }

        if qDict["Option-E"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-E"], style: UIAlertActionStyle.default, handler: nil))
        }
        alert.addAction(UIAlertAction(title: "Come back to this one", style: UIAlertActionStyle.destructive, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
    }

最佳答案

试试这个

  UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0 // change your self what you need

例如

let alert = UIAlertController(title: title,
                                  message: "dsfdsf",
                                  preferredStyle: UIAlertControllerStyle.alert)

    let cancelAction = UIAlertAction(title: "karthik tested for the word wrap text in alert",
                                     style: .cancel, handler: nil)

    alert.addAction(cancelAction)
    self.present(alert, animated: true, completion: nil)

      // number of lines
      UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0
      // for font
      UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).font = UIFont.systemFont(ofSize: 10.0)

输出

enter image description here

关于ios - Swift 3 中 UIAlertController 中的自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451799/

相关文章:

ios - 设置 "wait for executable to be launched"时 NSLog 不工作

iOS 强制 subview 具有灵活的高度

ios - 动画 UIView isHidden subview

ios - 将数组保存到 userDefault

ios - 尝试呈现其 View 不在窗口层次结构中的 UIAlertController (Swift 3/Xcode 8)

ios - 传递参数使代码运行 "slower"?

ios - 将启用时间限制为 UIDatePicker swift 4 中的特定时间跨度

xcode - 一键快速播放两种声音

ios - 不关闭 AlertController 的 UIAlertController 上的操作? (禁用解雇)

ios - 如何在 iOS 中展开过渡后显示警报?