sob*_*dio 5 iphone objective-c ipad ios
我有一个drawRect,使时间轴有点像iCal.我使用for循环沿滚动视图写入时间.我想知道A)是否是一种确定用户是否在系统设置中选择了12或24小时时钟的方法,以及B)是否有更有效的方式来更改时间标签,然后每次通过调用'if'查询'for'循环.干杯
较早的答案假设"AM"和"PM"符号用罗马字符表示.改编自keyur bhalodiya的代码在处理中文等语言方面做得更好,使用的方法AMSymbol和PMSymbol方法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)
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)
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)
| 归档时间: |
|
| 查看次数: |
2006 次 |
| 最近记录: |