If you look at the introductory book on application development, it's just a way to drag and drop the view onto the storyboard
.
That's fine, but for those who want to go one step further, I will write an article on how to implement the code.
First, let's define the variables.
let textField = UITextField()
Nothing is displayed even if you build in this state.
Well, it's only natural to declare a variable.
Next, let's put it in the view
of the view controller
.
view.addSubview(textField)
You can now add it. Let's build it. that? Nothing is displayed. Actually, there is this, but the width and height are 0. Let's specify the width and height.
// .init(x-axis position,Position in the y-axis direction,width,height)
textField.frame = .init(x: 0, y: 0, width: 200, height: 40)
//Displayed in the middle of the screen
textField.center = view.center
When you run it with this ~ It seems that there is nothing at first glance, but you can edit it by tapping the middle.
Let's also specify placeholder
.
textField.placeholder = "Enter here"
You can see that it is displayed properly. But it's hard to understand that there is no frame. Let's add a frame.
textField.layer.borderWidth = 1
textField.layer.borderColor = UIColor.black.cgColor
You're done! Let's make it look a little better.
//Round the corners
textField.layer.cornerRadius = 5
//The border line and the characters are too close, so separate them a little
textField.leftView = UIView(frame: .init(x: 0, y: 0, width: 5, height: 0))
textField.leftViewMode = .always
Build with this! The corners are rounded nicely and there is a gap between the border line and the letters.
//Turn off the function to correct English spelling without permission
textField.autocorrectionType = .no
//Turn off capitalizing the first letter of English without permission
textField.autocapitalizationType = .none
//Change font size
textField.font = .systemFont(ofSize: 18)
//Put textField in edit mode
textField.becomeFirstResponder()
//Release textField edit mode
textField.resignFirstResponder()
Please use it according to what you want to do. That's all for this time. Thank you for reading.
Recommended Posts