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

added custom colors output in Home View #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Pod/Classes/DRColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ extern CGFloat DRColorPickerJPEG2000Quality;
// new in iOS 8 is the concept of shared folders - if you want the color picker to use a shared folder accessible by apps
// with the same group id, set this to your group id, otherwise leave nil to use the documents folder. Default is nil
extern NSString* DRColorPickerSharedAppGroup;

// language code/name for lproj bundle,
// if given bundle not found, fall back to auto localization
extern NSString* DRColorPickerLocalizedLanguageCode;
18 changes: 18 additions & 0 deletions Pod/Classes/DRColorPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal
BOOL DRColorPickerUsePNG = NO;
CGFloat DRColorPickerJPEG2000Quality = 0.9f;
NSString* DRColorPickerSharedAppGroup = nil;
NSString* DRColorPickerLocalizedLanguageCode = nil;

NSBundle* DRColorPickerBundle() {
NSBundle *bundle = [NSBundle bundleForClass:DRColorPickerColor.class];
Expand All @@ -52,6 +53,23 @@ of this software and associated documentation files (the "Software"), to deal
NSString* DRCPTR(NSString* key, ...)
{
NSBundle *bundle = DRColorPickerBundle();

if (DRColorPickerLocalizedLanguageCode) {
NSString *path = [bundle pathForResource:DRColorPickerLocalizedLanguageCode ofType:@"lproj"];

if (path) {
// https://stackoverflow.com/a/7403767/3004003
NSBundle *langBundle = [NSBundle bundleWithPath:path];
NSString *stringsPath = [langBundle pathsForResourcesOfType:@"strings" inDirectory:nil].firstObject;
NSData *stringsData = [NSData dataWithContentsOfFile:stringsPath];
id plist = [NSPropertyListSerialization propertyListWithData:stringsData options:NSPropertyListImmutable format:nil error:nil];

if ([plist isKindOfClass:NSDictionary.class]) {
return plist[key];
}
}
}

NSString* result = NSLocalizedStringFromTableInBundle(key, @"DRColorPickerLocalizable", bundle, nil);
va_list ap;
va_start(ap, key);
Expand Down
3 changes: 2 additions & 1 deletion Pod/Classes/DRColorPickerHomeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
@interface DRColorPickerHomeView : UIView

@property (nonatomic, strong) DRColorPickerColor* color;
@property (nonatomic, strong) DRColorPickerGridView* standardColors;
@property (nonatomic, strong) DRColorPickerGridView* standardColorsGridView;
@property (nonatomic, strong) UIImage* addToFavoritesImage;
@property (nonatomic, weak) UIView* favoritesView;
@property (nonatomic, assign) BOOL showAlphaSlider;
@property (nonatomic, strong) NSArray<DRColorPickerColor*>* customColors;

@end
20 changes: 12 additions & 8 deletions Pod/Classes/DRColorPickerHomeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ - (id) initWithFrame:(CGRect)frame

_showAlphaSlider = YES;

self.standardColors = [[DRColorPickerGridView alloc] init];
[self addSubview:self.standardColors];
self.standardColorsGridView = [[DRColorPickerGridView alloc] init];
[self addSubview:self.standardColorsGridView];

self.alphaLabel = [[UILabel alloc] init];
self.alphaLabel.text = DRCPTR(@"Opacity", 1.0f);
Expand Down Expand Up @@ -111,7 +111,7 @@ - (void) setFrame:(CGRect)f

- (void) doLayout
{
if (self.standardColors == nil)
if (self.standardColorsGridView == nil)
{
return;
}
Expand Down Expand Up @@ -150,16 +150,20 @@ - (void) doLayout
}

CGFloat standardColorsY = CGRectGetMaxY(self.topDivider.frame);
self.standardColors.frame = CGRectMake(0.0f, standardColorsY, self.bounds.size.width, self.bounds.size.height - standardColorsY);
self.standardColorsGridView.frame = CGRectMake(0.0f, standardColorsY, self.bounds.size.width, self.bounds.size.height - standardColorsY);

[self createStandardColors];
if (self.customColors) {
self.standardColorsGridView.colors = self.customColors;
} else {
[self createStandardColors];
}
}

- (void) createStandardColors
{
NSMutableArray* colors = [NSMutableArray array];
[self.standardColors.drCollectionViewLayout calculatePages];
NSInteger colorCount = self.standardColors.drCollectionViewLayout.itemsPerPage;
[self.standardColorsGridView.drCollectionViewLayout calculatePages];
NSInteger colorCount = self.standardColorsGridView.drCollectionViewLayout.itemsPerPage;
NSInteger hueCount = (colorCount / 3) * 2;
NSInteger grayCount = colorCount - hueCount - 1; // -1 for transparency at the end
for (NSInteger i = 0; i < hueCount; i++)
Expand All @@ -173,7 +177,7 @@ - (void) createStandardColors
[colors addObject:[[DRColorPickerColor alloc] initWithColor:color]];
}
[colors addObject:[[DRColorPickerColor alloc] initWithColor:[UIColor clearColor]]];
self.standardColors.colors = colors;
self.standardColorsGridView.colors = colors;
}

- (void) animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
Expand Down
6 changes: 6 additions & 0 deletions Pod/Classes/DRColorPickerHomeViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ typedef void (^DRColorPickerDismissBlock)(BOOL cancel);
@property (nonatomic, strong) UIImage* wheelImage;
@property (nonatomic, strong) UIImage* importImage;

@property (nonatomic, strong) NSString *cancelButtonTitle;
@property (nonatomic, strong) NSString *doneButtonTitle;

// Custom colors for Home View
@property (nonatomic, strong) NSArray<DRColorPickerColor*>* customColors;

@end
31 changes: 28 additions & 3 deletions Pod/Classes/DRColorPickerHomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (id) init

__weak DRColorPickerHomeViewController* weakSelf = self;
self.homeView = [[DRColorPickerHomeView alloc] init];
self.homeView.standardColors.colorSelectedBlock = ^(DRColorPickerColor* color)
self.homeView.standardColorsGridView.colorSelectedBlock = ^(DRColorPickerColor* color)
{
DRColorPickerHomeViewController* strongSelf = weakSelf;
strongSelf.color = color;
Expand All @@ -89,8 +89,15 @@ - (void) viewDidLoad

[self loadDefaultImages];
[self createToolbar];
self.navigationItem.leftBarButtonItem = self.cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped:)];
self.navigationItem.rightBarButtonItem = self.doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];

if (!self.cancelButtonTitle) {
self.navigationItem.leftBarButtonItem = self.cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped:)];
}

if (!self.doneButtonTitle) {
self.navigationItem.rightBarButtonItem = self.doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];
}

self.navigationItem.title = DRCPTR(@"Colors");
self.homeView.frame = self.view.bounds;
[self.view addSubview:self.homeView];
Expand Down Expand Up @@ -338,6 +345,24 @@ - (void) setAddToFavoritesImage:(UIImage *)addToFavoritesImage
self.homeView.addToFavoritesImage = addToFavoritesImage;
}

- (void)setCancelButtonTitle:(NSString *)cancelButtonTitle
{
_cancelButtonTitle = cancelButtonTitle;
self.navigationItem.leftBarButtonItem = self.cancelButton = [[UIBarButtonItem alloc] initWithTitle:cancelButtonTitle style:UIBarButtonItemStylePlain target:self action:@selector(cancelTapped:)];
}

- (void)setDoneButtonTitle:(NSString *)doneButtonTitle
{
_doneButtonTitle = doneButtonTitle;
self.navigationItem.rightBarButtonItem = self.doneButton = [[UIBarButtonItem alloc] initWithTitle:doneButtonTitle style:UIBarButtonItemStyleDone target:self action:@selector(doneTapped:)];
}

- (void)setCustomColors:(NSArray<DRColorPickerColor *> *)customColors
{
_customColors = customColors;
self.homeView.customColors = customColors;
}

- (UIImage*) addToFavoritesImage
{
return self.homeView.addToFavoritesImage;
Expand Down
7 changes: 7 additions & 0 deletions Pod/Classes/DRColorPickerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@
+ (DRColorPickerViewController*) newColorPickerWithColor:(DRColorPickerColor*)color;

@property (nonatomic, strong, readonly) DRColorPickerHomeViewController* rootViewController;
// Custom colors for Home View
@property (nonatomic, strong) NSArray<DRColorPickerColor*>* customColors;
// Convenience property for customColors property
@property (nonatomic, strong) NSArray<UIColor*>* customUIColors;

@property (nonatomic, strong) NSString *cancelButtonTitle;
@property (nonatomic, strong) NSString *doneButtonTitle;

@end
30 changes: 30 additions & 0 deletions Pod/Classes/DRColorPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ - (DRColorPickerHomeViewController*) rootViewController
return (DRColorPickerHomeViewController*)self.viewControllers[0];
}

- (void)setCustomColors:(NSArray<DRColorPickerColor *> *)customColors
{
_customColors = customColors;
self.rootViewController.customColors = customColors;
}

- (void)setCustomUIColors:(NSArray<UIColor *> *)customUIColors
{
_customUIColors = customUIColors;
NSMutableArray *result = [NSMutableArray new];

for (UIColor *color in customUIColors) {
[result addObject:[[DRColorPickerColor alloc] initWithColor:color]];
}

self.customColors = result;
}

- (void)setCancelButtonTitle:(NSString *)cancelButtonTitle
{
_cancelButtonTitle = cancelButtonTitle;
self.rootViewController.cancelButtonTitle = cancelButtonTitle;
}

- (void)setDoneButtonTitle:(NSString *)doneButtonTitle
{
_doneButtonTitle = doneButtonTitle;
self.rootViewController.doneButtonTitle = doneButtonTitle;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
Expand Down