当用户输入时,我正在使用func格式化UITextField中的数字(单独数千个)。键盘是Eng时没关系,但当键盘改变Farsi时它什么也没做。我的代码:
override func viewdidload(){
self.mainTextField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
}
和
@objc func textFieldDidChange(textField: UITextField) {
if textField == self.mainTextField {
// Some locales use different punctuations.
var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
if let text = textFormatted, let textAsInt = Int(text) {
textField.text = numberFormatter.string(from: NSNumber(value: textAsInt))
}
}
}
答案 0 :(得分:0)
实际上,我不太确定实施的目的:
Mainwindow
但是,您需要设置所需的locale NumberFormatter。例如:
public class UserModel:BaseModel
{
string _Name;
public string Name
{
get
{
return _Name;
}
set
{
if (_Name != value)
{
_Name = value;
RaisePropertyChange("Name");
}
}
}
}
参考上面实现的内容,// Some locales use different punctuations.
var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")
将是一个包含波斯数字(“123,456”)的字符串,无论给定的文本是什么(“123456”)。
<强>更新强>
如果案例是:给定的输入是波斯语编号,所需的输出是英文编号,你可以这样做:
let text = "123456"
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "fa")
numberFormatter.numberStyle = .decimal
if let textAsInt = Int(text), let output = numberFormatter.string(from: NSNumber(value: textAsInt)) {
print(output) // "۱۲۳٬۴۵۶"
}