【问题标题】:Word Wrap in UIAlertController in Swift 3Swift 3 中 UIAlertController 中的自动换行
【发布时间】:2017-08-02 04:59:08
【问题描述】:

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

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

这是 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)
    }

【问题讨论】:

  • 显示你尝试过的代码
  • 我在图片下方添加了代码。感谢您的关注!

标签: ios swift3 uialertcontroller


【解决方案1】:

试试这个

  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)

输出

【讨论】:

  • 这是一个有趣的想法。我在哪里坚持这条线?在我上面发布的方法中?回答我自己的问题:是的,就是这样:)
  • 谢谢!!!在某个时候,我将尝试更改使用的字体,并更改所有其他操作的背景......但还没有:)
  • @zeeple - 我认为警报动作是不可能的,你只能改变 uialertcontroller 的背景
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
相关资源
最近更新 更多