luximetr/AnyFormatKit

Wrong caret calculation in SumTextInputFormatter

Closed this issue · 1 comments

In my app pattern for SumTextInputFormatter calculates by this small hack:

let f = NumberFormatter()
f.numberStyle = .currency
let source = f.string(from: NSNumber(value: 1234.56))
guard let pattern = source?.replacingOccurrences(of: "[0-9]", with: "#", options: .regularExpression) else {
    return
}
formatter = SumTextInputFormatter(textPattern: pattern)

So, for Russian locale on the device pattern will be # ###,## ₽ and for English locale, it will be $#,###.##. This hack adopts my app for multiple locales and ICU CLDR.

And here we are. The problem occurred when we use Russian locale and trying to delete characters with an amount greater than 1000 (In this case text formats with grouping symbol with space, like a "1 220"). When amount less than 1000 or locale is English – everything works fine.

Amount < 1000 – Works fine:
2019-08-07 12 12 32

Amount > 1000 – Doesn't work:
2019-08-07 12 13 11

Probably, it happened because there is a collision with group separator (space) and suffix (space + currency sign) for Ru locale