Change UITextField and UITextView Cursor / Caret Color

If you’re targeting iOS 7+, this has been made much easier. Simply change the tintColor of the field with a cursor using the appearance proxy and it will apply throughout the app: Swift 3.0: UITextField.appearance().tintColor = .black Objective-C: [[UITextField appearance] setTintColor:[UIColor blackColor]]; Same answer applies for an individual UITextField: Swift 3.0: myTextField.tintColor = .black Objective-C … Read more

How to hide keyboard in swift on pressing return key?

You can make the app dismiss the keyboard using the following function func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.view.endEditing(true) return false } Here is a full example to better illustrate that: // // ViewController.swift // // import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet var myTextField : UITextField override func viewDidLoad() { super.viewDidLoad() // … Read more

How do I check when a UITextField changes?

SWIFT Swift 4.2 textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged) and @objc func textFieldDidChange(_ textField: UITextField) { } SWIFT 3 & swift 4.1 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged) and func textFieldDidChange(_ textField: UITextField) { } SWIFT 2.2 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged) and func textFieldDidChange(textField: UITextField) { //your code } OBJECTIVE-C [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; and textFieldDidChange … Read more

Set padding for UITextField with UITextBorderStyleNone

I found a neat little hack to set the left padding for this exact situation. Basically, you set the leftView property of the UITextField to be an empty view of the size of the padding you want: UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways; Worked like a … Read more

Text inset for UITextField?

Overriding -textRectForBounds: will only change the inset of the placeholder text. To change the inset of the editable text, you need to also override -editingRectForBounds: // placeholder position – (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10); } // text position – (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10); }

Set the maximum character length of a UITextField

While the UITextField class has no max length property, it’s relatively simple to get this functionality by setting the text field’s delegate and implementing the following delegate method: Objective-C – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Prevent crashing undo bug – see note below. if(range.length + range.location > textField.text.length) { return NO; } NSUInteger … Read more

iOS – Dismiss keyboard when touching outside of UITextField

You’ll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it’s selector. The code: In viewDidLoad UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; In dismissKeyboard: -(void)dismissKeyboard { [aTextField resignFirstResponder]; } (Where aTextField is the textfield that is responsible for the keyboard) … Read more

iPhone UITextField – Change placeholder text color

Since the introduction of attributed strings in UIViews in iOS 6, it’s possible to assign a color to the placeholder text like this: if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) { UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}]; } else { NSLog(@”Cannot set placeholder text’s color, because deployment target is earlier than iOS 6.0″); … Read more

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