Format text field as user is typing

老子叫甜甜 提交于 2021-02-08 09:26:07

问题


I currently have two separate text fields in my app and I am trying to change it to only one. The text fields are used for inputting currency (one for pound, dollar, euro etc. and one for pence, cent etc.) and they work fine, however I need to add some functionality that won't be possible unless I use a single text field. Plus, I think a single text field is more user friendly.

Basically I would like to have a text field that formats in real time in currency form localised for the user while they are typing. Eg.

1 formats as £1.00 or $1.00 or 1.00€ etc... 1000 formats as £1,000.00 or $1,000.00 or 1 000,00€ etc...

I've done some research and found a few Objective-C examples, but I just can't figure out how to do the same thing in Swift (I've never programmed in Objective-C).

Any help would be very much appreciated as I have found this rather frustrating.


回答1:


You can use this code:

func currencyStringFromNumber(number: Double) -> String {
    let formatter = NSNumberFormatter()
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.currencyCode = NSLocale.currentLocale().displayNameForKey(NSLocaleCurrencySymbol, value: NSLocaleCurrencyCode)
    return formatter.stringFromNumber(number)
}

let currencyString = currencyStringFromNumber(21010)
println("Currency String is: \(currencyString)")

// Will print $21,010.00

Here's a compilable and working example of this information extrapolated to work as you require.

import UIKit

class CurrencyTextFieldExample: UIViewController {

    let currencyFormatter = NSNumberFormatter()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let textField = UITextField()
        textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
        textField.frame = CGRect(x: 0, y: 40, width: 320, height: 40)
        textField.keyboardType = UIKeyboardType.NumberPad
        textField.backgroundColor = UIColor.lightGrayColor()
        self.view.addSubview(textField)

        currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        currencyFormatter.currencyCode = NSLocale.currentLocale().displayNameForKey(NSLocaleCurrencySymbol, value: NSLocaleCurrencyCode)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldDidChange(textField: UITextField) {
        var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
        textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
    }

}

Let me know if you have questions.



来源:https://stackoverflow.com/questions/24977109/format-text-field-as-user-is-typing

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