xcode 如何启用仅数字数字键盘 ios Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49044028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable Only Numeric Digit Keyboard ios Swift
提问by Nasir Javed
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?
我正在开发 ios 应用程序,目前我正面临这个问题。我有三个文本字段,我希望用户在这些字段中输入数字,即 1 2 3 等,但在数字键盘中还有一个阿拉伯数字选项。有没有办法从键盘上删除阿拉伯数字选项?
回答by remyr3my
use this code
使用此代码
textfield.keyboardType = .asciiCapableNumberPad
回答by Abhinandan Pratap
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
将键盘类型更改为 Number Pad 。或支持 ascii 的数字键盘
回答by Waqas Sultan
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)
}
回答by Rocky
NSNumberFormatterformat 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.
NSNumberFormatter格式化包含NSNumber
对象的单元格的文本表示并将数值的文本表示转换为NSNumber
对象。您也可以更改数字格式和样式Locale
。
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
输出:86912881