iOS swift hide emoj icons in keyboard - ios

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

Related

Set the maximum amount limit on a UITextField in Swift

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
}

Swift prevent "0" being first character for 1 specific uiTextField

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
}

Dismiss keyboard after a user entered 11 digit [duplicate]

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
}

Swift: Detecting iOS Keyboard text replacement in UITextField

iOS has a built-in functionality where user can set up text replacements in
Settings > General > Keyboards > Text Replacement
In my Swift iOS app's UITextField, I need to know when user used this text replacement so I can take appropriate action. My instinct was to use
func textField(tf: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
but interestingly, this function is not invoked on this kind of text replacement, are there any other ways to solve this?
I tested the shouldChangeCharactersInRange function in this setting and it does see the text expansion. I tested with:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print(string.characters.count)
return true
}
You can watch the character count jump immediately after the text expansion.
Make sure your textField is hooked up with an IBOutlet and you have set the class as a delegate (textField.delegate = self in viewDidLoad).
Once you see this working you can of course identify text expansion and handle any way you wish.

Disable preview secure text entry UITextField

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
}

Resources