I am trying text field first character show lowercase, if I can type the upper case character also it has to be show the lower case. I tried this
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
signUpEmailTextField.autocapitalizationType = .none
}
But it's not working for me, my aim is when I type capital character also it should show the lower case in textfield.
A simple solution is to add target for when text changes and update text to lowercased there:
override func viewDidLoad() {
super.viewDidLoad()
signUpEmailTextField.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
}
#objc func textFieldChanged() {
signUpEmailTextField.text = signUpEmailTextField.text?.lowercased()
}
with uppercased() you can achieve this
you can also check condition if you have multiple textfields and you don't want uppercased in all text field
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text,
let range = Range.init(range, in: text) {
let newText = text.replacingCharacters(in: range, with: string).uppercased()
textField.text = newText
}
}
Related
i'm designing login and signUp screen for email-field i want restrict user to write Uppercase characters in textField.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (string.rangeOfCharacter(from: CharacterSet.uppercaseLetters) != nil) {
return false
}
return true
}
This code worked fine for input restriction.
But i want that user can't type it from keybord. For keybord i'm using IQKeyboardManagerSwift.
Please give me some solution for this.
Thank you.
textField.autocapitalizationType = .none
textfield.autocorrectionType = .no
textField.spellCheckingType = .no
Then:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
// Don't allow upper case letters
return false
}
return true
}
I've a number of textFields, I want to change text colour to white when user is typing them in text field. Following is my code with a lot of if conditions which doesn't seem to be efficient. Is there any way to do it without writing a lot of if conditions?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == emailTextField {
emailTextField.textColor = .white
} else if textField == nameTextField {
nameTextField.textColor = .white
} else if textField == addressTextField {
addressTextField.textColor = .white
}
return true
}
Just do
textField.textColor = .white
and whatever the textfield is it's textColor will be changed
Is there any way to detect cursor blinking in swift 4?
Actually, I wanna call the specific function after the user finished their editing, not while editing. i have used the function textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}, func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {} and all related functionalities. Those functionalities are called when the user typed 1 word but actually, I want to call the function after they typed the full words or chars they want.
Set a timer in shouldChangeCharactersInRange. When that fires, you'll know that there hasn't been any text input for whatever timeout value you choose:
private var timer: Timer?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
// this is called 1 second after the last entry in the textfield
}
return true
}
I want to perform some action when my textField character count becomes 6 in swift.
Use the delegate
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
and then implement
func textFieldDidChange(_ textField: UITextField) {
if textField.text?.characters.count == 6 {
// Call some method
}
}
You can use shouldChangeCharactersInRange to get length as well as position using range.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text ?? "") as String
let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
if (text.characters.count == 6 && range.location > 5) {
return false // if you want to restrict your string length of call some method here
}
return true
}
I'm using the following code to limit the amount of characters that can be in a UITextField
public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 44
}
This is done by setting the UITextField's delegate to self.
--
The issue here is that if you add an Emoji to the textfield, you cannot remove it. Even if you highlight - select all, or use "cut" the text will not change. The emoji can be before or after text, or even alone in the text field. You also cannot add two emojis to the field.
I don't see what the issue is here, can anyone help me out?
Its better if you don't do any checks inside shouldChange.. function and instead track the length separately using the UITextFieldTextDidChangeNotification notification. Sample code is attached below. In the shouldChange..just return a bool which is set in textDidChange. Here you will get the correct length. I noticed that in your method once you add an emoji it wont even allow you to type anymore. The shouldChangeCharactersInRange is not even getting called after entering a emoji char.
class ViewController: UIViewController, UITextFieldDelegate{
#IBOutlet weak var textField:UITextField!
var allow = true
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldDidChange:", name: UITextFieldTextDidChangeNotification, object: textField)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return allow
}
func textFieldDidChange(notification:NSNotification)
{
let length = textField.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
if length > 44 {
allow = false
} else {
allow = true
}
}
}
In Swift 3.1
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
let limit = 10
return currentCharacterCount < limit || string.characters.count < range.length
}
This won't prevent copy and paste long text string, I disabled the paste function for UITextField, with codes from
https://stackoverflow.com/a/39015132/6311644