I know how to put character length limit on UITextField. But I am looking for an idea how to put amount limit on UITextField so that it can not take more than that limited amount.
I want to put limit on TextField that can accept value in between 0 to 1000000
I tried to get it using UITextFieldDelegate's method
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let amount = Int(textField.text!) ,amount>1000000{
return false
}else{
return true
}
}
but I am not able to achieve the result.
You need to add the current string too with text field value.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text,
let amount = Int((text + string).trimmingCharacters(in: .whitespaces)),
(0 < amount), (amount < 1000000) {
return true
}
return false
}
Related
so i search online about how "func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool" for formating textField. i found this way to prevent "0" as my first char in my textfield. it works but, actually not the way i wanted.
The thing is, in my register view theres 4 uiTextfield, which contain :
Name
Phone Number
Pin
Confirm Pin
i only want to prevent "0" as my first char only for my PhoneNum TextField. but with this code, i kinda put all of those 4 textField into prevent "0" as the first char.
here's my code :
#IBOutlet weak var textFullName: SkyFloatingLabelTextField!
#IBOutlet weak var textPhoneNumber: SkyFloatingLabelTextField!
#IBOutlet weak var textPIN: SkyFloatingLabelTextField!
#IBOutlet weak var textConfirmPIN: SkyFloatingLabelTextField!
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// I try this one
if string == "0" {
if textField.text!.count == 0 {
return false
}
}
// i also try to change the "textField" into "textPhoneNumber"
if string == "0" {
if textPhoneNumber.text!.count == 0 {
return false
}
}
// both of those method wont work
return true
}
can someone tell me how to just prevent my textPhoneNumber to cannot type "0" as its first char? with this method i only put all of my textfield into cannot type "0" as its first char.
Thanks guys
The UITextFieldDelegate methods will get called for ALL of the text fields for which the view controller is set up as the delegate.
Presumably your textField(_:shouldChangeCharactersIn:replacementString:) method is being called for all 4 of your SkyFloatingLabelTextField objects (which I assume are a custom subclass of UITextField.)
If you want different behavior for different text fields, you need to write your code to handle each case separately. Something like this:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
switch textField {
case textPhoneNumber:
//Special case code to handle the phone number field
if string == "0" {
if textField.text!.count == 0 {
return false
}
return true
}
case textFullName:
//Special case code to handle the name field (if needed.)
default:
//Shared code to handle the other fields the same way
}
}
You can check if the textField is textPhoneNumber directly by using the equatability == sign.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == textPhoneNumber {
// do textPhoneNumber stuff
} else {
// others
}
return true
}
i am trying to hide emoji icons in iPhone default keyboard
keyboardtype = .ascicapbale
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.isFirstResponder{
if textField.textInputMode?.primaryLanguage == "emoji"{
return textField.textInputMode != nil
}
}
both solution not working for me. any help will appricated
I created a custom UITextField component for a fixed size input. TextField's width, font size, kerning and character count are all fixed. I can block user from entering more than 8 characters (see below shouldChangeCharactersIn method), however after the 8th character, cursor automatically moves to next position, which creates the following problem.
I want to show all the entered characters without decreasing their size. So
[ 1 2 3 4 5 6 7 8 ]
should be shown instead of
[ 2 3 4 5 6 7 8 | ]
I tried to put cursor just right of the 8th character, however this changes the all kerning and effects all of the other characters.
What is the correct way to handle this? How should I prevent the cursor movement, so that UITextField won't scroll right?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText),
!string.containsSpecialCharacters else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 8
}
I solved the problem by changing the kern after the last character. So now, instead of jumping 40pt, cursor jumps only 1pt (or whatever you set on below method)
#objc private func textFieldDidChange() {
guard let text = textField.text else { return }
if text.count == Constant.deviceCodeCharacterCount {
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.kern, value: Constant.deviceCodeCharacterSpacing, range: NSRange(location: 0, length: text.count - 1))
attributedString.addAttribute(.kern, value: 1, range: NSRange(location: Constant.deviceCodeCharacterCount - 1, length: 1))
textField.attributedText = attributedString
}
}
try the following code:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text?.count ?? 0 <= 7{
if textField.text?.count ?? 0 == 7{
DispatchQueue.main.async {
textField.resignFirstResponder()
}
}
return true
}
textField.resignFirstResponder()
return false
}
set your customTextField's delegate to self in your viewController's viewDidLoad method, and confirm to UITextFieldDelegate protocol
What happen if you try this code?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if range.location < 8 {
return true
}
// return NO to not change text
return false
}
This question already has answers here:
How to do a live UITextField count while typing (Swift)?
(12 answers)
Closed 4 years ago.
I've been looking for a solution in the site to dismiss keyboard after a user entered 11 digits. I found a Solution here, but it is an Objective-c and I'm looking for a swift code, can anybody help?
This is how it works
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// YOU SHOULD FIRST CHECK FOR THE BACKSPACE. IF BACKSPACE IS PRESSED ALLOW IT
if string == "" {
return true
}
if let characterCount = textField.text?.count {
// CHECK FOR CHARACTER COUNT IN TEXT FIELD
if characterCount >= 11 {
// RESIGN FIRST RERSPONDER TO HIDE KEYBOARD
return textField.resignFirstResponder()
}
}
return true
}
EDIT
1) You should set the IBOutlet to your textField
2) Set delegate to self, on your textField.
3) YourViewController: UIViewController, UITextFieldDelegate { }
4) Implement the delegate method as in above. check for the backspace and allow if user enters backspace to remove characters from textField.
you may try this :
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let count: Int = textField.text?.count ?? 0
if count >= 11 {
textField.resignFirstResponder()
}
return true
}
When an entry is made in into an UITextField, that has secure text entry enabled.
The last character that is input is given a short preview for a second or so.
I want to disable that as well. Thus, as soon as a character is entered the black dot appears without the preview.
This can be accomplished by configuring the field's UITextFieldDelegate like this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
return false
}