Multiline editable text UITextview inside UIAlertController?

試著忘記壹切 提交于 2019-11-28 03:53:39

问题


How can I make a multiline editable text UITextview inside a UIAlertController? UITextfield supports a single line and we need to make a multiline text edit box in the pop up window.


回答1:


This is good appraoch ...

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:{})


}



回答2:


I made a few tweaks to Vishal's approach.

  1. Added UITextViewDelegate method to display an initial light gray text as a placeholder and remove it on textViewDidBeginEditing.
  2. Set the textView border width, border color and background color to make it visible.

    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()
        }
    }
    }
    



回答3:


The following code works for Multiline UITextView in UIAlertController, Written in 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)



回答4:


You can't create multi-line textView in UIAlertController. You have to create your own UIAlertController for that.



来源:https://stackoverflow.com/questions/34420080/multiline-editable-text-uitextview-inside-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!