Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Only Numeric Digit Keyboard ios Swift

Tags:

xcode

ios

swift

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?

like image 980
Nasir Javed Avatar asked Mar 01 '18 06:03

Nasir Javed


4 Answers

use this code

textfield.keyboardType = .asciiCapableNumberPad
like image 180
remyr3my Avatar answered Sep 28 '22 06:09

remyr3my


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

enter image description here

like image 41
Abhinandan Pratap Avatar answered Sep 28 '22 06:09

Abhinandan Pratap


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)
}
like image 39
Waqas Sultan Avatar answered Sep 28 '22 04:09

Waqas Sultan


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

like image 20
Rocky Avatar answered Sep 28 '22 06:09

Rocky