I am working on ios app current I m facing the problem. I have three text fields and I want from the user to give numeric digit input in these fields i.e 1 2 3 etc.but in Numeric keypad there is also an option for Arabic digits . is there any way to remove Arabic digit option from keypad?
use this code
textfield.keyboardType = .asciiCapableNumberPad
Through Code you can do it by
override func viewDidLoad() {
super.viewDidLoad()
yourTextField.keyboardType = .numberPad //or .asciicapableNumberPad
}
OR
If you are using storyboard follow this
Change Keyboard type to Number Pad . or ascii capable Number Pad
You can use this function in delegate method.
public static func convertStringtoArabic(_ paramString: String) -> String {
var finalString: String = paramString
finalString = finalString.replacingOccurrences(of: "٠", with: "0")
finalString = finalString.replacingOccurrences(of: "١", with: "1")
finalString = finalString.replacingOccurrences(of: "٢", with: "2")
finalString = finalString.replacingOccurrences(of: "٣", with: "3")
finalString = finalString.replacingOccurrences(of: "٤", with: "4")
finalString = finalString.replacingOccurrences(of: "٥", with: "5")
finalString = finalString.replacingOccurrences(of: "٦", with: "6")
finalString = finalString.replacingOccurrences(of: "٧", with: "7")
finalString = finalString.replacingOccurrences(of: "٨", with: "8")
finalString = finalString.replacingOccurrences(of: "٩", with: "9")
return finalString
}
And Delegate Method you need to conform
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
newText = Preferences.convertStringtoArabic(newText)
// You can use this new text
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
NSNumberFormatter format the textual representation of cells that contain NSNumber
objects and convert textual representations of numeric values into NSNumber
objects.
You can change number formatting & style according to Locale
also.
Function to convert any number to specific locale.
func numberToLocale(number : String, localeIdentifier: String) -> NSNumber?{
let numberFormatter: NumberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: localeIdentifier)
guard let resultNumber = numberFormatter.number(from: number) else{
return nil
}
return resultNumber
}
Call to function
if let number = self.numberToLocale(number: "٨٦٩١٢٨٨١", localeIdentifier: "EN"){
print("number :", number)
}
Output : 86912881
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With