简体   繁体   English

本地化电话号码格式

[英]Localized phone number formatting

I've looked into NSFormatter , NSNumberFormatter , and the other formatting classes, but can't find a build into solution. 我查看了NSFormatterNSNumberFormatter和其他格式化类,但找不到构建到解决方案中。 I need to format phone numbers depending on the country code. 我需要根据国家/地区代码格式化电话号码。

For instance, for US, I get a string such as +16313938888 which I need to format to look like +1(631)393-8888 . 例如,对于美国,我得到一个字符串,如+16313938888 ,我需要格式化为+1(631)393-8888 The problem is I need to do this for all formats. 问题是我需要为所有格式执行此操作。 Netherlands, I receive a string +31641234567 which will be +31(6)41 23 45 67 (something like that). 荷兰,我收到一串+31641234567 ,这将是+31(6)41 23 45 67 (类似的东西)。

Hardcoding for 200+ countries is too tedious and I really don't know all the format rules. 200多个国家的硬编码太繁琐了,我真的不知道所有的格式规则。 Is there something in the docs I'm overlooking or does anyone know of an open source class that manages this? 在我正在忽略的文档中有什么东西,或者有人知道一个管理它的开源类吗?

有关iOS特定的解决方案,请参阅https://github.com/rmaddy/RMPhoneFormat

试试这个Google解决方案 - https://github.com/me2day/libPhoneNumber-iOS他们有C ++,Java,Objective-C等的端口。

Unfortunately iOS does not have any public APIs for this. 不幸的是,iOS没有任何公共API。 You can try to integrate libphonenumber that is a complete implementation for parsing and formatting international phone numbers. 您可以尝试集成libphonenumber ,它是解析和格式化国际电话号码的完整实现。 It has a C++ version so theoretically you can cross-link with it. 它有一个C ++版本,所以理论上你可以与它交叉链接。

You definitely don't want to hard-code all of the various country formats. 你绝对不想硬编码所有的国家格式。 There are typically 3-5 formats per country. 每个国家通常有3-5种格式。 Instead, use a format database (such as a plist) and write code to format the number based on the given country code. 而是使用格式数据库(例如plist)并编写代码以根据给定的国家/地区代码格式化数字。

A good international format property list 'UIPhoneFormats.plist' can be found here: https://code.google.com/p/iphone-patch/source/browse/trunk/bgfix/UIKit.framework/PhoneFormats/UIPhoneFormats.plist?r=7 一个好的国际格式属性列表'UIPhoneFormats.plist'可以在这里找到: https//code.google.com/p/iphone-patch/source/browse/trunk/bgfix/UIKit.framework/PhoneFormats/UIPhoneFormats.plist? R = 7

In that list, '$' allows any character, '#' must be a number, and the '(space) ', '(', ')' and '-' are inserted between numbers. 在该列表中,'$'允许任何字符,'#'必须是数字,'(空格)','(',')'和' - '插入数字之间。 Non-numeric characters typed by the user hint to the desired format. 用户键入的非数字字符提示为所需格式。

I've shared my phone number formatter class, inspired by Ahmed Abdelkader's work, at https://github.com/lathamglobal/iOS-Phone-Number-Formatter . 我在Ahps Abdelkader的工作中分享了我的电话号码格式化课程,地址是https://github.com/lathamglobal/iOS-Phone-Number-Formatter It is a very small, single-class international phone number formatter that uses the plist just mentioned. 它是一个非常小的单级国际电话号码格式化器,使用刚才提到的plist。

You can try this: 你可以试试这个:

    let phoneNumber : CNPhoneNumber
    let digits = phoneNumber.performSelector("digits").takeRetainedValue() as! String

It gives you directly the string, without formatting, with the phone number. 它直接为您提供电话号码的字符串,无需格式化。 However if the number is saved with international prefix, you will have it also in the resulted string. 但是,如果使用国际前缀保存号码,您也可以在结果字符串中使用该号码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM