Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to customize button fonts #17

Merged
merged 1 commit into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pod/Classes/PKDownloadButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 25 additions & 16 deletions Pod/Classes/PKDownloadButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}


Expand All @@ -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
Expand Down