iOS:模拟日历

发布于 2024-11-02 05:32:54 字数 683 浏览 0 评论 0原文

我有这段代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-11-09 05:32:54

循环是您的朋友:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:7];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc]       initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];

for (int i = 0; i < 31; i++) {
   NSTimeInterval seconds = 24*60*60 * i;
   NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
   NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
   int weekday = [weekdayComponents weekday];
   NSString *strDate = [dateFormatter stringFromDate: date];
   yourLabel.text = strDate;
   yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor];
}

请将 yourLabel 变量调整为适当的值。

Loops are your friend:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:7];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc]       initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];

for (int i = 0; i < 31; i++) {
   NSTimeInterval seconds = 24*60*60 * i;
   NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
   NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
   int weekday = [weekdayComponents weekday];
   NSString *strDate = [dateFormatter stringFromDate: date];
   yourLabel.text = strDate;
   yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor];
}

Please adjust the yourLabel variable to the appropriate ones.

酒儿 2024-11-09 05:32:54

这就像 @Eiko 的答案,除了在处理夏令时、没有 31 天的月份等时更正确:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setMonth:7];
[components setDay:1];

NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *startDate = [gregorianCalendar dateFromComponents:components];
NSRange rangeOfDays = [gregorianCalendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:startDate];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];

for (NSInteger i = 1; i <= NSMaxRange(rangeOfDays); ++i) {
  [components setDay:i];
  NSDate *date = [gregorianCalendar dateFromComponents:components];

  yourLabel.text = [dateFormatter stringFromDate: date];

  NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
  int weekday = [weekdayComponents weekday];

  yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor];
}

[dateFormatter release];

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:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setMonth:7];
[components setDay:1];

NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *startDate = [gregorianCalendar dateFromComponents:components];
NSRange rangeOfDays = [gregorianCalendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:startDate];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];

for (NSInteger i = 1; i <= NSMaxRange(rangeOfDays); ++i) {
  [components setDay:i];
  NSDate *date = [gregorianCalendar dateFromComponents:components];

  yourLabel.text = [dateFormatter stringFromDate: date];

  NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
  int weekday = [weekdayComponents weekday];

  yourLabel.textColor = weekday == 1 ? [UIColor redColor : blackColor];
}

[dateFormatter release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文