查找用户是否喜欢12/24小时时钟?
我有一个drawRect使时间线有点像iCal。 我使用for循环来写一个滚动视图的时间。 我想知道是否A)确定用户是否在系统设置中select了12或24小时的时钟,以及B)是否有更有效的方法来更改时间标签,然后每次调用“if”查询for的循环。 干杯
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);
较早的答案假设“AM”和“PM”符号以罗马字符表示。 这个由keyur bhalodiya改编的代码通过使用NSDateFormatter
的AMSymbol
和PMSymbol
方法在处理中文等语言方面PMSymbol
。
-(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); }
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 }