I've got a drawRect that makes a timeline a bit like iCal. I use a for loop to write the times along a scroll view. I was wondering if A) theres a way of determining whether the user has chosen a 12 or 24 hour clock in the system settings and B) if there is a more efficient way of changing the time labels then calling an 'if' query every pass of the 'for' loop. Cheers
我有一个绘制矩形使时间轴有点像iCal。我使用for循环在滚动视图中编写时间。我想知道A)是否有一种方法可以确定用户是否在系统设置中选择了12或24小时的时钟;干杯
3 个解决方案
#1
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);
#2
6
The earlier answers assume that the "AM" and "PM" symbols are represented in roman characters. This code adapted from keyur bhalodiya does a better job at handling languages like Chinese, by using the AMSymbol
and PMSymbol
methods of NSDateFormatter
.
早期的答案假设“AM”和“PM”符号用罗马字母表示。这个改编自keyur bhalodiya的代码通过使用NSDateFormatter的AMSymbol和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);
}
#3
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
}
#1
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);
#2
6
The earlier answers assume that the "AM" and "PM" symbols are represented in roman characters. This code adapted from keyur bhalodiya does a better job at handling languages like Chinese, by using the AMSymbol
and PMSymbol
methods of NSDateFormatter
.
早期的答案假设“AM”和“PM”符号用罗马字母表示。这个改编自keyur bhalodiya的代码通过使用NSDateFormatter的AMSymbol和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);
}
#3
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
}