Either one can be used
for getting user input but a general thumb rule to follow is
- For entering a single line of text use UITextField.
- For displaying or entering one or more lines of text use UITextView.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Main View"
addUIViews()
}
func addUIViews(){
let myTextField = UITextField()
myTextField.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myTextField)
let myTextView = UITextView()
myTextView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myTextView)
let views = [
"view" : view,
"textField" : myTextField,
"textView" : myTextView
]
var allConstraints: [NSLayoutConstraint] = []
allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[textField(50)]-[textView]-|",
options: [], metrics: nil, views: views as [String : Any])
allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-[textField(200)]-|",
options: [], metrics: nil, views: views as [String : Any])
allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-[textView(200)]-|",
options: [], metrics: nil, views: views as [String : Any])
NSLayoutConstraint.activate(allConstraints)
myTextField.layer.borderWidth = 1.0
myTextField.layer.cornerRadius = 5.0
myTextField.font = .systemFont(ofSize: 24)
myTextView.layer.borderWidth = 1.0
myTextView.layer.cornerRadius = 5.0
myTextView.font = .systemFont(ofSize: 24)
}
}