how to use persian numbers in UItextField in swift 3?

▼魔方 西西 提交于 2020-01-11 13:16:06

问题


Hi I am using text field in my swift3 application - I want when user enter a number in the text field instead of english number textfield shows persian numbers - my textfield keyboard has set to just enter number so the user can't use text inside the text field with keyboard so here is my codes that doesn't work

  override func viewDidLoad() {
         super.viewDidLoad()
 let numberFormatter = NumberFormatter()
    numberFormatter.locale = Locale(identifier: "fa")
    numberFormatter.numberStyle = NumberFormatter.Style.decimal
    let cm:Int? = Int(englishNumber.text!)

    if cm != nil {
    numberFormatter.string(from: NSNumber(value: cm!))
    myTextField.text = numberFormatter.string(from: NSNumber(value: cm!))!
    }

**remember that I used textfield Delegate and I want every Number that user entered change to Persian number and the user thought that writing Persian Numbers **


回答1:


try this :

 func convertEngNumToPersianNum(num: String)->String{
        let number = NSNumber(value: Int(num)!)
        let format = NumberFormatter()
        format.locale = Locale(identifier: "fa_IR")
        let faNumber = format.string(from: number)

        return faNumber!
    }


来源:https://stackoverflow.com/questions/44627982/how-to-use-persian-numbers-in-uitextfield-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!