Getting and Setting Cursor Position of UITextField and UITextView in Swift

The following content applies to both UITextField and UITextView. 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? = textField.selectedTextRange Get cursor position if let selectedRange = textField.selectedTextRange { … Read more

Super slow lag/delay on initial keyboard animation of UITextField

Before you implement any exotic hacks to get around this problem, try this: stop the debug session, close the app from multitasking, unplug your device from the computer and run the app normally by tapping its icon. I have seen at least two cases in which the delay only occurs while the device is plugged … Read more

Having a UITextField in a UITableViewCell

Try this out. Works like a charm for me (on iPhone devices). I used this code for a login screen once. I configured the table view to have two sections. You can of course get rid of the section conditionals. – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; if (cell == … Read more

Programmatically change UITextField Keyboard type

There is a keyboardType property for a UITextField: typedef enum { UIKeyboardTypeDefault, // Default type for the current input method. UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation. UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently). UIKeyboardTypeNumberPad, // … Read more

Adding the “Clear” Button to an iPhone UITextField

This button is a built-in overlay that is provided by the UITextField class, but as of the iOS 2.2 SDK, there isn’t any way to set it via Interface Builder. You have to enable it programmatically. Add this line of code somewhere (viewDidLoad, for example): Objective-C myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing; Swift 5.0 myUITextField.clearButtonMode = .whileEditing

Create space at the beginning of a UITextField

This is what I am using right now: Swift 4.2 class TextField: UITextField { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) override open func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: padding) } override open func placeholderRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: padding) } override open func editingRect(forBounds … Read more

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

This has been classified as “works as intended” by Apple engineers. I filed a bug for this a while back. Their reasoning is that the user is often going to be entering data in a modal form so they are trying to be “helpful” and keep the keyboard visible where ordinarily various transitions within the … Read more

Move textfield when keyboard appears swift

There are a couple of improvements to be made on the existing answers. Firstly the UIKeyboardWillChangeFrameNotification is probably the best notification as it handles changes that aren’t just show/hide but changes due to keyboard changes (language, using 3rd party keyboards etc.) and rotations too (but note comment below indicating the keyboard will hide should also … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)