Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Last active November 28, 2018 14:33
Show Gist options
  • Save erkanyildiz/4a4c08f79970feaa73b5 to your computer and use it in GitHub Desktop.
Save erkanyildiz/4a4c08f79970feaa73b5 to your computer and use it in GitHub Desktop.
Convenience label for displaying app name, app version and copyright dates automatically
// erkanyildiz
// 20161004-1321+0900
//
// EYVersionLabel.h
#import <UIKit/UIKit.h>
@interface EYVersionLabel : UILabel
@property (nonatomic, strong) NSNumber* initialYear;
@end
// erkanyildiz
// 20161004-1321+0900
//
// EYVersionLabel.m
#import "EYVersionLabel.h"
@implementation EYVersionLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self commonInit];
}
return self;
}
- (void)commonInit
{
self.numberOfLines = 2;
self.textAlignment = NSTextAlignmentCenter;
NSCalendar *calendar = [NSCalendar.alloc initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger year = [components year];
[self setInitialYear:@(year)];
}
- (void)setInitialYear:(NSNumber *)year
{
//NOTE: Directly can be set in InterfaceBuilder as User Defined Runtime Attribute
// e.g. Key Path, Type, Value
// InitialYear, Number, 2015
NSString* version = [NSString stringWithFormat:@"%@ v%@", NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"], NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"]];
NSCalendar *calendar = [NSCalendar.alloc initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger currentYear = [components year]; NSInteger initialYear = year.integerValue;
NSString* copyright = [NSString stringWithFormat:@"\nCopyright © %li", (long)initialYear];
if(initialYear < currentYear)
copyright = [copyright stringByAppendingFormat:@" - %li", (long)currentYear];
NSMutableAttributedString * as = [NSMutableAttributedString.alloc initWithString:version attributes:nil];
[as appendAttributedString:[NSMutableAttributedString.alloc initWithString:copyright attributes:@{NSFontAttributeName:[self.font fontWithSize:self.font.pointSize*0.7]}]];
self.attributedText = as;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment