Add lefthand margin to UITextField

You can do it by extending UITextField class and overriding two methods: – (CGRect)textRectForBounds:(CGRect)bounds; – (CGRect)editingRectForBounds:(CGRect)bounds; Here is the code: The interface in MYTextField.h @interface MYTextField : UITextField @end Its implementation in MYTextField.m @implementation MYTextField static CGFloat leftMargin = 28; – (CGRect)textRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; return bounds; } – (CGRect)editingRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; … Read more

How can I restrict special characters in UITextField except dot and underscores?

Try code block given below, it worked fine for me. SWIFT 3.0 let ACCEPTABLE_CHARACTERS = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_” func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let cs = NSCharacterSet(charactersIn: ACCEPTABLE_CHARACTERS).inverted let filtered = string.components(separatedBy: cs).joined(separator: “”) return (string == filtered) } Objective C #define ACCEPTABLE_CHARACTERS @” ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.” – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range … Read more

Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

In my app, I have successfully used a combination of contentInset and scrollToRowAtIndexPath like this: When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height: tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0); Then, you can safely use [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] animated:YES]; By adding the contentInset, … Read more

Why Is UITextField.text An Optional?

This is a historical thing. UITextField does not make any difference between an empty string and a nil string. In Objective-C there was no need to make a difference between them because you can call methods on nil in Objective-C. Also, there was no way in Objective-C to prevent users from assigning nil to a … Read more

Disabling automatic scrolling of UITableView when editing UITextField inside UITableViewCell

The autoscroll-behavior is located in the UITableViewController functionality. To disable the automatic scrolling I found two ways: Use instead of the UITableViewController simply a UIViewController – set the datasource and delegate on your own. Override the viewWillAppear method and don’t call [super viewWillAppear: animated] With both solution you disable not only the Autoscroll, but also … Read more

UITextField: move view when keyboard appears

This solution is based on ComSubVie’s one. Advantages: It supports device rotation – works for all orientations; It doesn’t hardcode the values for animation duration and curve, it reads them from the keyboard notification; It utilizes UIKeyboardWillShowNotification instead of UIKeyboardDidShowNotification to sync keyboard animation and custom actions; It doesn’t use the deprecated UIKeyboardBoundsUserInfoKey; It handles … Read more

How to change the tint color of the clear button on a UITextField

Here you go! A TintTextField. Using no custom image, or added buttons etc. class TintTextField: UITextField { var tintedClearImage: UIImage? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setupTintColor() } override init(frame: CGRect) { super.init(frame: frame) self.setupTintColor() } func setupTintColor() { self.borderStyle = UITextField.BorderStyle.roundedRect self.layer.cornerRadius = 8.0 self.layer.masksToBounds = true self.layer.borderColor = self.tintColor.cgColor self.layer.borderWidth = 1.5 … Read more