检测iPhone是否在12小时或24小时模式下显示时间? [英] Detect whether iPhone is displaying time in 12-Hour or 24-Hour Mode?
问题描述
如何检测iPhone是否在12小时或24小时模式下显示时间?
How can you detect whether iPhone is displaying time in 12-Hour or 24-Hour Mode?
我目前正在监控当前区域的变化,但这不是仅限影响时间显示的设置。日期和时间的24小时切换时间设置会覆盖当前区域设置。
I currently monitor the current region for changes, however that's not the only setting that affects how times are displayed. The 24-hour toggle in the date & time settings overrides the current regions settings.
有没有办法检测此12/14小时设置?
Is there any way to detect this 12/14 hour setting?
推荐答案
我已经找到了一个很好的方法来确定这一点,我已经添加了一个 NSLocale
的类别。它似乎相当准确,并且在测试多个地区时没有发现任何问题。
I've figured out a decent way of determining this with a little function I've added to a category of NSLocale
. It appears to be pretty accurate and haven't found any problems with it while testing with several regions.
@implementation NSLocale (Misc)
- (BOOL)timeIs24HourFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24Hour = amRange.location == NSNotFound && pmRange.location == NSNotFound;
[formatter release];
return is24Hour;
}
@end
这篇关于检测iPhone是否在12小时或24小时模式下显示时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!