最近公司做的项目需要使用日期选择器,但是ios自带的感觉使用不是很方便,而且太普通了点。所以在网上搜了些日期选择器,发现一款叫做CKcalendar的开源项目挺不错的。项目下载地址[url]https://github.com/jaykz52/CKCalendar[/url]。运行工程,显示效果如下
[img]http://dl.iteye.com/upload/attachment/0073/7820/6a1c1e2c-6be4-3480-8898-b5e8d6e27121.png[/img]
但是在使用之后发现这个开源的项目根本就没有提供接口让我们将里面的英文改成中文使用。好吧,我自己汉化 :cry: ,代码我来了 :cry: 。
之后发现是用 dayOfWeekLabel 这个变量来显示星期。故只要在为其赋值之前将其汉化即可。
汉化方法:
月份的汉化同样的道理,找到相应的标签,然后对其过滤。代码如下:
过滤方法:
汉化后的运行效果如下:
[img]http://dl.iteye.com/upload/attachment/0073/7845/2997e8f5-1030-3f6e-9bc3-26b6f1451240.png[/img]
最后附上整个工程的代码:
[b][color=red]转载请说明出处[url]http://ff20081528.iteye.com/blog/1678018[/url][/color][/b]
[img]http://dl.iteye.com/upload/attachment/0073/7820/6a1c1e2c-6be4-3480-8898-b5e8d6e27121.png[/img]
但是在使用之后发现这个开源的项目根本就没有提供接口让我们将里面的英文改成中文使用。好吧,我自己汉化 :cry: ,代码我来了 :cry: 。
之后发现是用 dayOfWeekLabel 这个变量来显示星期。故只要在为其赋值之前将其汉化即可。
for (NSString *day in [self getDaysOfTheWeek]) {
UILabel *dayOfWeekLabel = [[UILabel alloc] initWithFrame:CGRectZero];
dayOfWeekLabel.text = [[self weekToChinese:day] uppercaseString];//拦击过滤
dayOfWeekLabel.textAlignment = UITextAlignmentCenter;
dayOfWeekLabel.backgroundColor = [UIColor clearColor];
dayOfWeekLabel.shadowColor = [UIColor whiteColor];
dayOfWeekLabel.shadowOffset = CGSizeMake(0, 1);
[labels addObject:dayOfWeekLabel];
[self.calendarContainer addSubview:dayOfWeekLabel];
}
汉化方法:
#pragma - mark 自定义方法 用于汉化日历的星期
- (NSString *) weekToChinese:(NSString *) enWeek {
if (!([enWeek length] > 0)) {
return nil;
}
NSString *upperWeek = [enWeek uppercaseString];
NSString *chWeek = nil;
if ([upperWeek isEqualToString:@"MON"]) {
chWeek = @"周一";
} else if([upperWeek isEqualToString:@"TUE"]) {
chWeek = @"周二";
} else if([upperWeek isEqualToString:@"WED"]) {
chWeek = @"周三";
} else if([upperWeek isEqualToString:@"THU"]) {
chWeek = @"周四";
} else if([upperWeek isEqualToString:@"FRI"]) {
chWeek = @"周五";
} else if([upperWeek isEqualToString:@"SAT"]) {
chWeek = @"周六";
} else if([upperWeek isEqualToString:@"SUN"]) {
chWeek = @"周日";
}
return chWeek;
}
月份的汉化同样的道理,找到相应的标签,然后对其过滤。代码如下:
- (void)setMonthShowing:(NSDate *)aMonthShowing {
_monthShowing = [self firstDayOfMonthContainingDate:aMonthShowing];
self.titleLabel.text = [self monthToChinese:[self.dateFormatter stringFromDate:_monthShowing]];//过滤汉化
[self setNeedsLayout];
}
过滤方法:
#pragma - mark 自定义方法 用于汉化日历的月份
- (NSString *) monthToChinese:(NSString *) enMonth {
if (!([enMonth length][NSMutableString alloc]init];
NSArray *arr = [enMonth componentsSeparatedByString:@" "];
NSString *arrMonth = [arr objectAtIndex:0];
NSString *arrYear = [arr objectAtIndex:1];
if ([arrMonth isEqualToString:@"January"]) {
[chMonth appendString:@"一月"];
} else if([arrMonth isEqualToString:@"February"]) {
[chMonth appendString:@"二月"];
} else if([arrMonth isEqualToString:@"March"]) {
[chMonth appendString:@"三月"];
} else if([arrMonth isEqualToString:@"April"]) {
[chMonth appendString:@"四月"];
} else if([arrMonth isEqualToString:@"May"]) {
[chMonth appendString:@"五月"];
} else if([arrMonth isEqualToString:@"June"]) {
[chMonth appendString:@"六月"];
} else if([arrMonth isEqualToString:@"July"]) {
[chMonth appendString:@"七月"];
} else if([arrMonth isEqualToString:@"August"]) {
[chMonth appendString:@"八月"];
} else if([arrMonth isEqualToString:@"September"]) {
[chMonth appendString:@"九月"];
} else if([arrMonth isEqualToString:@"October"]) {
[chMonth appendString:@"十月"];
} else if([arrMonth isEqualToString:@"November"]) {
[chMonth appendString:@"十一月"];
} else if([arrMonth isEqualToString:@"December"]) {
[chMonth appendString:@"十二月"];
}
[chMonth appendFormat:@" %@", arrYear];
return chMonth;
}
汉化后的运行效果如下:
[img]http://dl.iteye.com/upload/attachment/0073/7845/2997e8f5-1030-3f6e-9bc3-26b6f1451240.png[/img]
最后附上整个工程的代码:
[b][color=red]转载请说明出处[url]http://ff20081528.iteye.com/blog/1678018[/url][/color][/b]