Swift 3 中 UIAlertController 的「换行」功能

6
我正在使用Swift 3构建一个问答应用程序,利用UITableViewController列出问题,我想使用Alert 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)
    }

展示你尝试过的代码 - Anbu.Karthik
我已经在图片下面添加了代码。谢谢您的关注! - zeeple
1个回答

8

试一下这个

  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


这是一个有趣的想法。我该把那一行放在哪里?是放在我上面发布的方法中吗?回答我的问题:是的,它应该放在那里 :) - zeeple
谢谢!!!在某个时候,我将尝试更改所使用的字体,并且更改每个其他操作的背景...但现在还不行 :) - zeeple
@zeeple - 我认为警报操作不可能,你只能更改uialertcontroller的背景。 - Anbu.Karthik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接