文章转载自:http://www.th7.cn/Program/IOS/2011/11/16/46929.shtml
与CFLocale自由转换.可以通过注册NSCurrentLocaleDidChangeNotification 通知来获得区域位置改变消息.
生成
- – initWithLocaleIdentifier:
- + systemLocale// 操作系统的,最初始的区域?
- + currentLocale// 当前用户设定区域,肯能是缓存的
- + autoupdatingCurrentLocale// 当前用户的系统区域设置,实时更新的
信息
- – displayNameForKey:value:
/*
某种locale中对指定区域中指定属性的显示值。并不是所有的属性都有显示值。
举例:
|
NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease]; |
|
NSString *displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]; |
|
NSLog(@"displayNameString fr_FR: %@", displayNameString); |
|
displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]; |
|
NSLog(@"displayNameString en_US: %@", displayNameString); |
输出
|
displayNameString fr_FR: français (France) |
|
displayNameString en_US: anglais (États-Unis) |
另一种
|
NSLocale *gbLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]; |
|
displayNameString = [gbLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]; |
|
NSLog(@"displayNameString fr_FR: %@", displayNameString); |
|
displayNameString = [gbLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]; |
|
NSLog(@"displayNameString en_US: %@", displayNameString); |
返回
|
displayNameString fr_FR: French (France) |
|
displayNameString en_US: English (United States) |
*/
- – localeIdentifier// 返回locale的标识
- – objectForKey:
系统信息
- + availableLocaleIdentifiers// 返回NSString的NSArray,设备支持的区域
- + ISOCountryCodes// 所有的ISO定义的国家地区编码
- + ISOCurrencyCodes// 所有的ISO定义的货币编码
- + ISOLanguageCodes// 所有ISO定义的语言编码
- + commonISOCurrencyCodes// 通用的货币编码
与标识转换
- + canonicalLocaleIdentifierFromString:// 从给定的local标识中返回标准的标识
- + componentsFromLocaleIdentifier:
/*
从字符串中解析出一个local的组成信息.
参数
string
一个local ID 包含 language, script, country, variant, and keyword/value pairs,比如,"en_US@calendar=japanese".
返回一个解析得到的字典,上面的可以解析出: NSLocaleLanguageCode=en,NSLocaleCountryCode=US, and NSLocaleCalendar=NSJapaneseCalendar.
*/
- + localeIdentifierFromComponents:
/*
与上面的函数对应。
比如字典{NSLocaleLanguageCode="en", NSLocaleCountryCode="US", NSLocaleCalendar=NSJapaneseCalendar}解析成"en_US@calendar=japanese".
*/
- + canonicalLanguageIdentifierFromString:// 给定标识返回标准的语言支持标识
- + localeIdentifierFromWindowsLocaleCode:// 从Windows的地区码得到locale标识
- + windowsLocaleCodeFromLocaleIdentifier:// 从locale标识得到Windows的地区码
首选语言
- + preferredLanguages// 用户可选的语言集,IETF BCP 47中定义
文字方向
- + characterDirectionForLanguage:// 返回指定语言的字符排列方向
- + lineDirectionForLanguage:// 指定语言的行排列方向
一些变量
NSLocaleLanguageDirection
These constants describe the text direction for a language. Used by the methodslineDirectionForLanguage: and characterDirectionForLanguage:.
enum {
NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,// 未知
NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight,
NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft,
NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom,
NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop
};
typedef NSUInteger NSLocaleLanguageDirection;
NSLocale Component Keys
NSLocale 的组成key
NSString * const NSLocaleIdentifier;// 标识
NSString * const NSLocaleLanguageCode;// 语言码zh
NSString * const NSLocaleCountryCode;// 区域码
NSString * const NSLocaleScriptCode;// The key for the locale script code.
NSString * const NSLocaleVariantCode;// The key for the locale variant code.
NSString * const NSLocaleExemplarCharacterSet;// 元字符集 NSCharacterSet
NSString * const NSLocaleCalendar;// 当地日历 NSCalendar
NSString * const NSLocaleCollationIdentifier;// The key for the collation associated with the locale.
NSString * const NSLocaleUsesMetricSystem;// 是否使用公制 NSNumber
NSString * const NSLocaleMeasurementSystem;// 当地度量单位
NSString * const NSLocaleDecimalSeparator;// 小数点符号
NSString * const NSLocaleGroupingSeparator;// 分组符号
NSString * const NSLocaleCurrencySymbol;// 货币符号
NSString * const NSLocaleCurrencyCode;// 货币代码
NSString * const NSLocaleCollatorIdentifier;// 具体语言码??zh-Hans
NSString * const NSLocaleQuotationBeginDelimiterKey;// 引号开始符号
NSString * const NSLocaleQuotationEndDelimiterKey;// 引号结束符号
NSString * const NSLocaleAlternateQuotationBeginDelimiterKey;// 单引号开始符号
NSString * const NSLocaleAlternateQuotationEndDelimiterKey;// 单引号结束符号
NSLocale Calendar Keys
NSCalendar 包含的.
NSString * const NSGregorianCalendar;// 公历标识
NSString * const NSBuddhistCalendar;// 佛历标识
NSString * const NSChineseCalendar;// 农历标识(不支持)
NSString * const NSHebrewCalendar;// 希伯来历
NSString * const NSIslamicCalendar;// 回历
NSString * const NSIslamicCivilCalendar;
NSString * const NSJapaneseCalendar;// 日本历
NSString * const NSRepublicOfChinaCalendar;// 民国历法(不能用来格式,有些函数不正确?)
NSString * const NSPersianCalendar;// 波斯历法
NSString * const NSIndianCalendar;// 印度历法
NSString * const NSISO8601Calendar;// ISO8601历法,标准还没制定?
上面的可用来创建NSCalendar或者由NSCalendar的标识函数得到.
通知
NSCurrentLocaleDidChangeNotification
用户的区域设置改变事件.
测试 自己的理解
//返回所有合法国家编码
NSArray *countryCodes = [NSLocale ISOCountryCodes];
for (NSString *countryCode in countryCodes)
{
NSLog(@"countryCode = %@",countryCode);
//获取国家编码的国家标志
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSLog(@"identifier = %@",identifier);
NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
NSLog(@"country = %@",country);
[_items addObject: country];
}
本文详细介绍了 iOS 中 NSLocale 的使用方法,包括初始化、获取显示值、解析与转换标识符等功能,并提供了实例代码。同时,还列举了 NSLocale 的常用属性及系统支持的编码类型。

3816

被折叠的 条评论
为什么被折叠?



