NSNumberFormatter分组

时间:2016-05-26 09:06:53

标签: ios swift nsnumberformatter

我想将带有数字的字符串格式化为:XX XX XX(例如12 34 56

我使用NSNumberFormatter分组分隔符:

let formatter = NSNumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSize = 2
formatter.groupingSeparator = " "
if let number = Int(theText.stringByReplacingOccurrencesOfString(" ", withString: "")) {
       let numberToFormat = NSNumber(integer:number)
       textField.text = formatter.stringFromNumber(numberToFormat)
}

如果输入数字123456,这样可以正常工作 - 格式为12 34 56

我在shouldChangeCharactersInRange方法中使用它,因此它应该在您键入时格式化数字。分组分隔符定义的数字按右侧的groupingSize值进行分组,因此当您键入123456时,这就是文本更改的方式:

1
12
1 23
12 34
1 23 45
12 34 45

我想将其格式化如下:

1
12
12 3
12 34
12 34 5
12 34 56

是否可以定义从哪一侧分组分隔符组编号?

2 个答案:

答案 0 :(得分:1)

不确定是否必须转换为Int,如何添加" 0"在末尾?然后删除最后一个" 0"。

let formatter = NSNumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSize = 2
formatter.groupingSeparator = " "

var str = "12345"
if str.characters.count % 2 != 0 {
    str += "0"
}
if let number = Int(str.stringByReplacingOccurrencesOfString(" ", withString: "")) {
    let numberToFormat = NSNumber(integer:number)
    formatter.stringFromNumber(numberToFormat)
}

另一种解决方案:

let string = "12345"

var results = [String]()
for index in 0 ..< string.characters.count-1 {
    if index % 2 == 0 {
        let range = NSRange(location: index, length: 2)
        results.append((string as NSString).substringWithRange(range))
    }
}

if string.characters.count % 2 != 0 {
    let last = (string as NSString).substringFromIndex(string.characters.count-1)
    results.append(last)
}

答案 1 :(得分:0)

机智4

您必须使用UITextField的委托:func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)

这很好:

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == YourTextField {
        if (textField.text?.count)! < 8 {
            let formatter = NumberFormatter()
            formatter.usesGroupingSeparator = true
            formatter.locale = NSLocale(localeIdentifier: "en_GB") as Locale?
            formatter.groupingSize = 2
            formatter.groupingSeparator = "-" // if you want the format XX-XX-XX
            // Uses the grouping separator corresponding
            if let groupingSeparator = formatter.groupingSeparator {

                if string == groupingSeparator {
                    return true
                }
                if let textWithoutGroupingSeparator = textField.text?.replacingOccurrences(of: groupingSeparator, with: "") {
                    var totalTextWithoutGroupingSeparators = textWithoutGroupingSeparator + string
                    if string == "" { // pressed Backspace key
                        totalTextWithoutGroupingSeparators.characters.removeLast()
                    }
                    if let numberWithoutGroupingSeparator = formatter.number(from: totalTextWithoutGroupingSeparators),
                        let formattedText = formatter.string(from: numberWithoutGroupingSeparator) {
                        print(numberWithoutGroupingSeparator)
                        numberWithoutGroupingSeparators = String(format: "%@", numberWithoutGroupingSeparator)
                        textField.text = formattedText
                        return false
                    }
                }
            }
            return true
        }
    }
        return true

}