本文介绍了NSNumberFormatter格式化美国电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将用户输入的一串数字转换成一个性感的字符串,如iPhone上的Phone.app。这里是我使用的代码,它不工作(没有特殊的格式出来),在一定数量的数字后,它刚开始添加0到字符串的结尾。

I'm trying to convert a string of numbers, entered by the user, into a sexy string like Phone.app on the iPhone. Here is the code I'm using, which doesn't work (no special format comes out) and after a certain number of digits it just starts adding "0" to the end of the string.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setPositiveFormat:@"+# (###) ###-###"];
[formatter setLenient:YES];
NSString *strDigits = [self stringByReplacingOccurrencesOfRegex:@"[^0-9+]" withString:@""];
return [formatter stringFromNumber:[NSNumber numberWithDouble:[strDigits doubleValue]]];


推荐答案

我认为你的问题是 NSNumberFormatter 不支持括号,空格或破折号。我试图实现和你一样的方法,它失败,只是输出未格式化的文本。

I think your issue is that NSNumberFormatter does not support brackets, spaces or dashes. I tried to implement the same method as you and it failed silently and just output unformatted text.

这里的一般问题是iPhone SDK不提供方法以区域设置依赖的方式格式化电话号码。

The general problem here is that the iPhone SDK doesn't provide a method to format phone numbers in a locale dependent way.

我为Apple提出了以下问题(其中两个是已知问题的重复,所以我已经包括苹果的原始bug#):

I have raised bugs with Apple for the following (two of these were duplicates of known issues so I've included Apple's original bug # for those):

#6933244 - Need iPhone SDK interface to format text as locale dependent phone number
#5847381 - UIControl does not support a setFormatter method
#6024985 - Overridden UITextField drawTextInRect method is never called

NSPhoneNumberFormatter ,您将在 UIControl 上调用 setFormatter 它以一个漂亮的方式显示 text 。不幸的是,在iPhone上不存在。

In an ideal world Apple would provide an NSPhoneNumberFormatter, and you would call setFormatter on your UIControl so it displayed text in a nice pretty way. Unfortunately that doesn't exist on the iPhone.

这篇关于NSNumberFormatter格式化美国电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:53
查看更多