我正在使用 swift 3 构建一个测验应用程序,它利用 UITableViewController 列出问题,我想使用的是一个警报 View 来发布问题,并提供多种答案供您选择。我让它以我想要的方式工作,但有一个巨大的怪癖,那就是长答案的字体要小得多,并且通过用省略号替换答案中间的文本来截断。
如何在 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)
}
最佳答案
试试这个
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)
输出
关于ios - Swift 3 中 UIAlertController 中的自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451799/