Tutorial by Examples

Swift let frame = CGRect(x: 0, y: 0, width: 100, height: 100) let textField = UITextField(frame: frame) Objective-C CGRect *frame = CGRectMake(0, 0, 100, 100); UITextField *textField = [[UITextField alloc] initWithFrame:frame]; Interface Builder You can also add a UITextField to a storybo...
Add an accessory view above the keyboard. This is commonly used for adding next/previous buttons, or additional buttons like Done/Submit (especially for the number/phone/decimal pad keyboard types which don't have a built-in return key). Swift let textField = UITextField() // initialized however ...
Swift textField.autocapitalizationType = .None Objective-C textField.autocapitalizationType = UITextAutocapitalizationTypeNone; All options: .None \ UITextAutocapitalizationTypeNone : Don't autocapitalize anything .Words \ UITextAutocapitalizationTypeWords : Autocapitalize every word .S...
Swift Ctrl + Drag from the UItextfield in MainStoryboard to the ViewController Class and create a UITextField Outlet After that select the UItextField again and Ctrl+drag in ViewController class but this time select Action connection and on storage select Did End On Exit then click connect. ...
Swift textField.textAlignment = .Center Objective-C [textField setTextAlignment: NSTextAlignmentCenter]; In the example, we have set the NSTextAlignment to center. You can also set to .Left, .Right, .Justified and .Natural. .Natural is the default alignment for the current localization. Th...
To change the appearance of the keyboard, the following types can be set individually on every UITextFields property: keyboardType typedef NS_ENUM(NSInteger, UIKeyboardType) { UIKeyboardTypeDefault, // Default type for the current input method. UIKeyboardTypeASCIICapable, ...
Observe the notifications UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, update the scrollView content insets according to keyboard height, then scroll to the focused control. - (void)viewDidLoad { [super viewDidLoad]; // register for keyboard notifications [[...
Get Focus Swift textField.becomeFirstResponder() Objective-C [textField becomeFirstResponder]; Resign Swift textField.resignFirstResponder() Objective-C [textField resignFirstResponder];
In some cases, you want to show your users a UIPickerView with predefined contents for a UITextField instead of a keyboard. Create a custom UIPickerView At first, you need a custom wrapper-class for UIPickerView conforming to the protocols UIPickerViewDataSource and UIPickerViewDelegate. class My...
Setup your view controller to manage editing of text for the text field. class MyViewController: UITextFieldDelegate { override viewDidLoad() { super.viewDidLoad() textField.delegate = self } } textFieldShouldReturn is called every time the return butto...
Useful information The very beginning of the text field text: let startPosition: UITextPosition = textField.beginningOfDocument The very end of the text field text: let endPosition: UITextPosition = textField.endOfDocument The currently selected range: let selectedRange: UITextRange? = tex...
To hide the blinking caret, you need to override caretRectForPosition of a UITextField and return CGRectZero. Swift 2.3 < public override func caretRectForPosition(position: UITextPosition) -> CGRect { return CGRectZero } Swift 3 override func caretRect(for position: UITextPositio...
We can change the style of the placeholder by setting attributedPlaceholder (a NSAttributedString). var placeholderAttributes = [String: AnyObject]() placeholderAttributes[NSForegroundColorAttributeName] = color placeholderAttributes[NSFontAttributeName] = font if let placeholder = textField.p...
Initialize the UITextField with a CGRect as a frame: Swift let textfield = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 21)) Objective-C UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)]; You can also create a UITextField in Interface Builde...

Page 1 of 1