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.
![iOS Swift difference between UITextField and UITextView](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeOesCUeh_gnz0d3HKEGowtXHdgLBf2gW1GhDaeitd4M_UH4McXYP4G9YeRcX5sRhM0SJjU4D5iDHl-QzFdEHz6Z1H2r4IkMJWTw1DshzxD6lkuuuGKeKMYjqv28VJb44uXJ6ZTI2lGkg/s640/Simulator+Screen+Shot+-+iPhone+XR+-+2019-05-29+at+17.00.33.png)
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)
}
}
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.