Apoorv Blog

Currency Format Input to UITextfield in Swift

Updated on:



Screenshot Tutorial

1. First let’s setup the XCode project as usual. Create new single view application X code project. Set project name to currency textfield and save it to the desktop.

Set project name

2. Go to main storyboard and add text field into the view controller.

add text field

3. Select text field and set its constraint to 150 on top, 50 on leading and trailing each.

add text field constraints

4. Open assistant editor and create outlet for currency text field.

add text field outlet

5. Below view did load create action for textfield changed and its very important to set action as editing changed.

add text field action

6. Select text field and sets its alignment to right sided. Also set its keyboard type to Number Pad.

add text field formatting

7. Add lazy initialized number formatter with number style currency. You can customize currency based on your requirements.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    @IBOutlet weak var currencyTextField: UITextField!
    
    lazy var currencyFormatter: NumberFormatter = {
        
        let formatter = NumberFormatter()
        
        formatter.numberStyle = .currency
        
        formatter.locale = Locale(identifier: "en_US")
        
//        formatter.currencyCode = "USD"
        
//        formatter.currencySymbol = "$"
        
        formatter.maximumFractionDigits = 2
        
        formatter.minimumFractionDigits = 2
        
        return formatter
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

8. Set place holder for textfield to zero currency using the number formatter.

34
35
36
37
38
39
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        currencyTextField.placeholder = currencyFormatter.string(from: NSNumber(value: 0))
    }

9. First get text from textfield. Then map over the string & convert each character into string then into integer. The nil values will be eliminated with compact map. Then reduce array of integers into single integer with reduce function. Then convert integer into double and divide by 100 to get fraction values.

41
42
43
44
45
46
47
48
49
50
    @IBAction func textFieldChanged(_ sender: UITextField) {
        
        guard let text = sender.text else { return }
        
        let array = text.compactMap({ Int(String($0)) })
        
        let num = Double(array.reduce(0, {($0 * 10) + $1})) / 100
        
        currencyTextField.text = currencyFormatter.string(from: NSNumber(value: num))
    }

10. Finally build and run on iPhone XR.

demo currency textfield

11. Challenge: - Set upper limit for textfield so you cannot add currency more than billion and when currency is zero set textfield to empty.

challenge currency textfield

comments powered by Disqus