背景
最近做项目涉及到12小时/24小时制切换的问题,网上有一些现成判断算法,但是在涉及到语言和地区切换的时候都会存在问题。因此自己研究了一番,终于找到了相对完美的方案。
问题
算法一:
+ (BOOL)is12HourFormat{
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA =[formatStringForHours rangeOfString:@"a"];
BOOL hasAMPM =containsA.location != NSNotFound;
return hasAMPM;
}
这是网上最主流的算法,但是在日语等区域的时候就会失效,比如日语区无论时间是12还是24小时制formatStringForHours始终为"H時",其他地区没仔细测试了。
算法二:
+ (BOOL)is12HourFormat{
NSLocale *local = [NSLocale autoupdatingCurrentLocale];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
calendar.locale = local;
NSString *amSymbol = [calendar AMSymbol];
NSString *pmSymbol = [calendar PMSymbol];
NSString *dateStr = [[NSDate date] descriptionWithLocale:local];
// NSLog(@"dateStr:%@-amSymbol:%@-pmSymbol:%@",dateStr,amSymbol,pmSymbol);
BOOL is12Hour = NO;
for (NSString *symbol in @[amSymbol,pmSymbol]) {
if ([dateStr rangeOfString:symbol].location != NSNotFound) {
is12Hour = YES;
break;
}
}
return is12Hour;
}
这个算法是我参考网上一个算法改进的,讲道理我觉得这个算法逻辑没什么问题,但是NSString *dateStr = [[NSDate date] descriptionWithLocale:local]
获取到的描述有时候跟amSymbol
pmSymbol
对不上。而且[NSLocale autoupdatingCurrentLocale]
[NSLocale currentLocale]
好像都不能及时获取到local的更改,测试过程中好几次都是获取到上一次的loacl值。
终极方案
在测试过程中,虽然一、二算法都存在问题,但是发现iOS状态栏上显示时间的是能根据是否12小时制及时切换的,也不存在语言地区问题,不知道为什么iOS不像Android那样有提供这个的系统api。
既然状态栏能实时切换,那么如果获取到状态的时间的字符串,那么这个问题就迎刃而解了。
+ (BOOL)is12HourFormat{
UIApplication *app = [UIApplication sharedApplication];
NSString *timeStrValue = nil;
@try {
if (isNotchMobile) {
id statusBar = [[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"statusBar"];
id data = [statusBar valueForKeyPath:@"currentData"];
id timeEntry = [data valueForKeyPath:@"timeEntry"];
timeStrValue = [timeEntry valueForKeyPath:@"stringValue"];
}else{
id statusBar = [app valueForKeyPath:@"statusBar"];
NSArray *subviews = [[statusBar valueForKeyPath:@"foregroundView"] subviews];
id statusBarTimeItemView = nil;
for (id subview in subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarTimeItemView")]) {
statusBarTimeItemView = subview;
}
}
if (@available(iOS 12.0, *)) {
timeStrValue = [statusBarTimeItemView valueForKeyPath:@"dateTimeString"];
}else{
timeStrValue = [statusBarTimeItemView valueForKeyPath:@"timeString"];
}
}
} @catch (NSException *exception) {
} @finally {
if (!timeStrValue) {
return 算法一或算法二的结果;
}
}
// 判断是否字符串中只存在数字、空格、冒号,如果是则表示为24小时制,否则为12小时制(存在AM/PM/下午/上午等内容)
NSCharacterSet *numberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789 :"] invertedSet];
NSString *filtered = [[timeStrValue componentsSeparatedByCharactersInSet:numberSet] componentsJoinedByString:@""];
BOOL is12Hour = ![timeStrValue isEqualToString:filtered];
return is12Hour;
}
#define isNotchMobile ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size)||CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size)||CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size)) : NO)