Skip to content

Instantly share code, notes, and snippets.

@nfarina
Created November 17, 2011 15:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfarina/1373480 to your computer and use it in GitHub Desktop.
Save nfarina/1373480 to your computer and use it in GitHub Desktop.
Useful functions for drawing NSDate in the exact style of Apple's iOS Mail app.
/*
The MIT License
Copyright (c) 2010 Nick Farina
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
@interface NSDate (PrettyDrawing)
- (NSString *)dayWhen; // "Today", "Yesterday", "Tuesday", "5/5/09", etc.
// Draws a timestamp in the exact style of Mail/Messages list items. "8:25 PM" if today, "Yesterday", "Tuesday", "5/5/09" if older.
- (CGSize)drawInRect:(CGRect)rect; // Fonts match Mail/Messages
- (CGSize)drawInRect:(CGRect)rect withDateFont:(UIFont *)dateFont timeFont:(UIFont *)timeFont amPmFont:(UIFont *)amPmFont;
@end
#import "NSDate+Drawing.h"
// Safe to be static - drawing is only done from the main thread
static NSDateFormatter *timeFormatter = nil;
static NSDateFormatter *amPmFormatter = nil;
static NSDateFormatter *dayFormatter = nil;
static NSDateFormatter *dateFormatter = nil;
static NSNumberFormatter *numberFormatter = nil;
static NSDateFormatter *oldFormatter = nil;
static NSCalendar *gregorian = nil;
void NSDatePrettyDrawingEnsureFormatters() {
if(!timeFormatter) {
timeFormatter = [NSDateFormatter new];
[timeFormatter setDateFormat:@"h:mm"];
amPmFormatter = [NSDateFormatter new];
[amPmFormatter setDateFormat:@"a"];
dayFormatter = [NSDateFormatter new];
[dayFormatter setDateFormat:@"EEEE"];
dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"d"];
numberFormatter = [NSNumberFormatter new];
oldFormatter = [NSDateFormatter new];
[oldFormatter setDateFormat:@"M/d/yyyy"];
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
}
}
@implementation NSDate (PrettyDrawing)
- (NSString *)dayWhen {
NSDatePrettyDrawingEnsureFormatters();
// if the date is older than 6 days ago, print it in M/d/yyyy format.
// This date API is RUBBISH.
NSDate *today = [NSDate date];
// Get the time components of the current date
NSDateComponents *timeComponents = [gregorian components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
NSDateComponents *componentsToSubtract = [[[NSDateComponents alloc] init] autorelease];
[componentsToSubtract setDay:-6];
[componentsToSubtract setHour:0-[timeComponents hour]];
[componentsToSubtract setMinute:0-[timeComponents minute]];
[componentsToSubtract setSecond:0-[timeComponents second]];
NSDate *sevenDaysAgo = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
if ([self timeIntervalSinceReferenceDate] < [sevenDaysAgo timeIntervalSinceReferenceDate])
return [oldFormatter stringFromDate:self];
// date is current enough to say things like Today, Yesterday, Friday
NSInteger selfDate = [[numberFormatter numberFromString:[dateFormatter stringFromDate:self]] intValue];
NSInteger nowDate = [[numberFormatter numberFromString:[dateFormatter stringFromDate:[NSDate date]]] intValue];
if (selfDate == nowDate)
return @"Today";
else if (nowDate - 1 == selfDate)
return @"Yesterday";
else
return [dayFormatter stringFromDate:self];
}
- (CGSize)drawInRect:(CGRect)rect {
return [self drawInRect:rect withDateFont:[UIFont systemFontOfSize:14] timeFont:[UIFont boldSystemFontOfSize:14]
amPmFont:[UIFont systemFontOfSize:12]];
}
- (CGSize)drawInRect:(CGRect)rect withDateFont:(UIFont *)dateFont timeFont:(UIFont *)timeFont amPmFont:(UIFont *)amPmFont {
NSString *day = [self dayWhen];
if ([day isEqual:@"Today"]) {
rect.origin.y += 2;
CGSize amPmSize =
[[amPmFormatter stringFromDate:self]
drawInRect:rect withFont:amPmFont lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
rect.size.width -= amPmSize.width + 2;
rect.origin.y -= 2;
CGSize timeSize =
[[timeFormatter stringFromDate:self]
drawInRect:rect withFont:timeFont lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
CGSize result;
result.width = amPmSize.width + timeSize.width + 2;
result.height = amPmSize.height + timeSize.height;
return result;
}
else
return [day drawInRect:rect withFont:dateFont lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment