查找用户是否更喜欢12/24小时时钟?

sob*_*dio 5 iphone objective-c ipad ios

我有一个drawRect,使时间轴有点像iCal.我使用for循环沿滚动视图写入时间.我想知道A)是否是一种确定用户是否在系统设置中选择了12或24小时时钟的方法,以及B)是否有更有效的方式来更改时间标签,然后每次通过调用'if'查询'for'循环.干杯

Wil*_*iss 6

较早的答案假设"AM"和"PM"符号用罗马字符表示.改编自keyur bhalodiya的代码在处理中文等语言方面做得更好,使用的方法AMSymbolPMSymbol方法NSDateFormatter.

-(BOOL)uses24hourTime
{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setLocale:[NSLocale currentLocale]];
     [formatter setDateStyle:NSDateFormatterNoStyle];
     [formatter setTimeStyle:NSDateFormatterShortStyle];

     NSString *dateString = [formatter stringFromDate:[NSDate date]];
     NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
     NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];

     return (amRange.location == NSNotFound && pmRange.location == NSNotFound);
}
Run Code Online (Sandbox Code Playgroud)


Amy*_*all 5

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];

if([[dateFormatter dateFormat] rangeOfString:@"a"].location != NSNotFound) {
    // user prefers 12 hour clock
} else {
    // user prefers 24 hour clock
}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是最好的答案,因为许多国家将AM和PM指定为上午和下午. (2认同)

Den*_*nis 1

NSDate *today = [NSDate date];
NSString *formattedString = [NSDateFormatter localizedStringFromDate:today dateStyle: kCFDateFormatterNoStyle timeStyle: kCFDateFormatterShortStyle];

NSRange foundRange;
foundRange = [formattedString rangeOfString:"am" options:NSCaseInsensitiveSearch];
if(foundRange.location == NSNotFound) {
    foundRange = [formattedString rangeOfString:"pm" options:NSCaseInsensitiveSearch];
}

BOOL isAMPMSettingOn = (foundRange.location != NSNotFound);
Run Code Online (Sandbox Code Playgroud)