iOS:模拟日历
我有这段代码
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:7];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *strDate = [dateFormatter stringFromDate: date];
,但现在我想为每月的每一天设置 31 个标签:如何为每个标签添加此组件?
[components setDay:1];
我还希望所有的星期日都是红色的,那么平均 7 天的标签必须是红色的;你能帮助我吗?
I have this code
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:7];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *strDate = [dateFormatter stringFromDate: date];
but now I want set 31 labels for each day of my month: how I can add this component for each label?
[components setDay:1];
and I also I want that all sundays are red, then avery 7 days label must be red color; can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
循环是您的朋友:
请将
yourLabel
变量调整为适当的值。Loops are your friend:
Please adjust the
yourLabel
variable to the appropriate ones.这就像 @Eiko 的答案,除了在处理夏令时、没有 31 天的月份等时更正确:
This is like @Eiko's answer, except more correct when dealing with things like daylight savings time, months that don't have 31 days, etc: