Skip to content

Instantly share code, notes, and snippets.

@davidyeiser
Last active December 24, 2015 12:09
Show Gist options
  • Save davidyeiser/6796330 to your computer and use it in GitHub Desktop.
Save davidyeiser/6796330 to your computer and use it in GitHub Desktop.
The code I use to display a date label in my weather app.
- (void)displayCurrentDate
{
// This grabs the current date and initializes it to
// the variable "now"
NSDate *now = [[NSDate alloc] init];
// This initializes the class that turns NSDate objects
// into readable text strings
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// Sets how the date will be represented (EEEE is a
// code for full day name)
[dateFormat setDateFormat:@"EEEE"];
// Saves the full day name to the string "currentDayName"
NSString* currentDayName = [[dateFormat stringFromDate:now] uppercaseString];
// Sets how the date will be represented for the rest
// of the date
[dateFormat setDateFormat:@", MMMM dd, yyyy"];
// Saves it to the string "currentDayDate"
NSString* currentDayDate = [dateFormat stringFromDate:now];
// This combines both strings above into one
currentDay = [NSString stringWithFormat:@"%@%@", currentDayName, currentDayDate];
// The following sets up and then applies the formatting
// to the attributed string.
// Sets the bold font (this could be called anything,
// strongFont, bold, bolded, etc.)
UIFont *boldFont = [UIFont fontWithName:@"Metric-Semibold" size:16.0];
// Sets the regular font
UIFont *regularFont = [UIFont fontWithName:@"Metric-Regular" size:17.0];
// Sets the color for the text
UIColor *textColor = [UIColor colorWithRed:(51/255.f) green:(51/255.f) blue:(51/255.f) alpha:1.0];
// This sets tracking, I don't think it uses any of
// the font's built-in formulas for kerning though.
// From what I read I think it just adds a set amount
// of space between each glyph.
NSNumber *tracking = [NSNumber numberWithFloat:1.0];
// This sets up the attributes to be applied to the
// entire string.
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName,
textColor, NSForegroundColorAttributeName, nil];
// These will be applied to a subset of the string
// after the above is applied. As you can guess from
// the variable names, this is how we'll set our
// bold font
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName,
tracking, NSKernAttributeName, nil];
// The way it works is you tell it to apply a specific
// formatting style for a specific range of the string.
// What we do here is make a range that goes from the
// start of the string (0) to the end of the day name
// (currentDayName.length).
const NSRange range = NSMakeRange(0, currentDayName.length);
// Now we initialize our attributed string with its
// contents set to the combined NSString currentDay
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:currentDay
attributes:attrs];
// Then we apply the bolded style attributes
// (subAttrs) to the attributed string for the
// aforementioned range
[attributedText setAttributes:subAttrs range:range];
// Now we set our finalized attributed string as
// the text for the current day label.
[currentDayField setAttributedText:attributedText];
// And I like to set the background color on labels
// explicitly because when I first started building
// this app it seemed like the code for displaying
// labels was getting run multiple times, thereby
// placing labels on top of each other. It doesn't
// seem to do it anymore, but I'm still a bit leery.
// I think this is what you call Cargo cult programming.
[currentDayField setBackgroundColor:[UIColor whiteColor]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment