From 4e25a27a8d8bf1e9d6901d17a77c1c783be91c0e Mon Sep 17 00:00:00 2001 From: Ryan Holden Date: Wed, 31 Aug 2016 13:21:14 -0400 Subject: [PATCH] Add API to customize button fonts --- Pod/Classes/PKDownloadButton.h | 2 ++ Pod/Classes/PKDownloadButton.m | 41 +++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Pod/Classes/PKDownloadButton.h b/Pod/Classes/PKDownloadButton.h index e8be562..cf14e31 100644 --- a/Pod/Classes/PKDownloadButton.h +++ b/Pod/Classes/PKDownloadButton.h @@ -46,5 +46,7 @@ IB_DESIGNABLE -(void)updateStartDownloadButtonText:(NSString *)title; -(void)updateDownloadedButtonText:(NSString *)title; +-(void)updateStartDownloadButtonText:(NSString *)title font:(UIFont *)font; +-(void)updateDownloadedButtonText:(NSString *)title font:(UIFont *)font; @end \ No newline at end of file diff --git a/Pod/Classes/PKDownloadButton.m b/Pod/Classes/PKDownloadButton.m index 05f3739..d8758e8 100644 --- a/Pod/Classes/PKDownloadButton.m +++ b/Pod/Classes/PKDownloadButton.m @@ -12,16 +12,6 @@ #import "UIImage+PKDownloadButton.h" #import "PKPendingView.h" -static NSDictionary *DefaultTitleAttributes(UIColor *color) { - return @{ NSForegroundColorAttributeName : color, - NSFontAttributeName : [UIFont systemFontOfSize:14.f]}; -} - -static NSDictionary *HighlitedTitleAttributes() { - return @{ NSForegroundColorAttributeName : [UIColor whiteColor], - NSFontAttributeName : [UIFont systemFontOfSize:14.f]}; -} - @interface PKDownloadButton () @property (nonatomic, weak) PKBorderedButton *startDownloadButton; @@ -98,8 +88,8 @@ - (instancetype)initWithFrame:(CGRect)frame { - (void)tintColorDidChange { [super tintColorDidChange]; - [self updateButton:self.startDownloadButton title:self.startDownloadButton.titleLabel.text]; - [self updateButton:self.downloadedButton title:self.downloadedButton.titleLabel.text]; + [self updateButton:self.startDownloadButton title:[self.startDownloadButton titleForState:UIControlStateNormal] font:self.startDownloadButton.titleLabel.font]; + [self updateButton:self.downloadedButton title:[self.downloadedButton titleForState:UIControlStateNormal] font:self.downloadedButton.titleLabel.font]; } @@ -113,11 +103,30 @@ -(void)updateDownloadedButtonText:(NSString *)title { [self updateButton:self.downloadedButton title:title]; } + +-(void)updateStartDownloadButtonText:(NSString *)title font:(UIFont *)font { + [self updateButton:self.startDownloadButton title:title font: font]; +} + +-(void)updateDownloadedButtonText:(NSString *)title font:(UIFont *)font { + [self updateButton:self.downloadedButton title:title font: font]; +} + + - (void)updateButton:(UIButton *)button title:(NSString *)title { - NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:title attributes:DefaultTitleAttributes(self.tintColor)]; - [button setAttributedTitle:attrTitle forState:UIControlStateNormal]; - NSAttributedString *highlitedTitle = [[NSAttributedString alloc] initWithString:title attributes:HighlitedTitleAttributes()]; - [button setAttributedTitle:highlitedTitle forState:UIControlStateHighlighted]; + [self updateButton:button title:title font:[UIFont systemFontOfSize:14.f]]; +} + +- (void)updateButton:(UIButton *)button title:(NSString *)title font:(UIFont *)font { + if (title == nil) { + title = @""; + } + + [button setTitle:title forState:UIControlStateNormal]; + [button setTitleColor:self.tintColor forState:UIControlStateNormal]; + [button setTitleColor:UIColor.whiteColor forState:UIControlStateHighlighted]; + + button.titleLabel.font = font; } #pragma mark - private methods