UIAlertController 内的多行可编辑文本 UITextview?
Posted
技术标签:
【中文标题】UIAlertController 内的多行可编辑文本 UITextview?【英文标题】:Multiline editable text UITextview inside UIAlertController? 【发布时间】:2015-12-22 16:16:41 【问题描述】:如何在UIAlertController
中创建多行可编辑文本UITextview
? UITextfield
支持单行,我们需要在弹出的窗口中做一个多行文本编辑框。
【问题讨论】:
【参考方案1】:这是一个很好的方法......
func popUpController()
let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
let customView = UITextView(frame: rect)
customView.backgroundColor = UIColor.clear
customView.font = UIFont(name: "Helvetica", size: 15)
// customView.backgroundColor = UIColor.greenColor()
alertController.view.addSubview(customView)
let somethingAction = UIAlertAction(title: "Something", style: UIAlertAction.Style.default, handler: (alert: UIAlertAction!) in print("something")
print(customView.text)
)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: (alert: UIAlertAction!) in print("cancel"))
alertController.addAction(somethingAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion:)
【讨论】:
我对此做了一些研究。似乎 UIAlertController 的内部实现已经改变(我正在使用 ios9),这将不起作用。仅供参考。 不要这样做。 Apple 谈到 UIAlertController:"The view hierarchy for this class is private and must not be modified."
【参考方案2】:
以下代码适用于UIAlertController
中的多行UITextView
,编写于Swift 4
let alert = UIAlertController(title: "Enter your summary", message: "\n\n\n\n\n\n\n\n", preferredStyle: .alert)
alert.view.autoresizesSubviews = true
let textView = UITextView(frame: CGRect.zero)
textView.translatesAutoresizingMaskIntoConstraints = false
let leadConstraint = NSLayoutConstraint(item: alert.view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: -8.0)
let trailConstraint = NSLayoutConstraint(item: alert.view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 8.0)
let topConstraint = NSLayoutConstraint(item: alert.view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: -64.0)
let bottomConstraint = NSLayoutConstraint(item: alert.view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 64.0)
alert.view.addSubview(textView)
NSLayoutConstraint.activate([leadConstraint, trailConstraint, topConstraint, bottomConstraint])
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: action in
print("\(String(describing: textView.text))")
))
present(alert, animated: true)
【讨论】:
【参考方案3】:我对 Vishal 的方法做了一些调整。
-
添加了 UITextViewDelegate 方法以将初始浅灰色文本显示为占位符并在 textViewDidBeginEditing 上将其删除。
设置textView的边框宽度、边框颜色和背景颜色使其可见。
class ViewController: UIViewController, UITextViewDelegate
...
@IBAction func showAlert(sender: AnyObject)
let alertController = UIAlertController(title: "Hello, I'm alert! \n\n\n\n\n\n\n", message: "", preferredStyle: .Alert)
let rect = CGRectMake(15, 50, 240, 150.0)
let textView = UITextView(frame: rect)
textView.font = UIFont(name: "Helvetica", size: 15)
textView.textColor = UIColor.lightGrayColor()
textView.backgroundColor = UIColor.whiteColor()
textView.layer.borderColor = UIColor.lightGrayColor().CGColor
textView.layer.borderWidth = 1.0
textView.text = "Enter message here"
textView.delegate = self
alertController.view.addSubview(textView)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let action = UIAlertAction(title: "Ok", style: .Default, handler: action in
let msg = (textView.textColor == UIColor.lightGrayColor()) ? "" : textView.text
print(msg)
)
alertController.addAction(cancel)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: )
func textViewDidBeginEditing(textView: UITextView)
if textView.textColor == UIColor.lightGrayColor()
textView.text = ""
textView.textColor = UIColor.darkGrayColor()
【讨论】:
非常感谢。这正是我想要的。但是开始编辑时它不会上升。你有解决方案吗?【参考方案4】:您不能在UIAlertController
中创建多行文本视图。你必须为此创建自己的UIAlertController
。
【讨论】:
谢谢 这是我自己定制的好方法,最后我在我的回答中做到了。非常感谢给我一个很好的链接。以上是关于UIAlertController 内的多行可编辑文本 UITextview?的主要内容,如果未能解决你的问题,请参考以下文章
如何使我在 UIAlertController 内的 UITextField 中输入的数据通过 Swift 中的重置持续存在?