diff --git a/Pod/Classes/DRColorPicker.h b/Pod/Classes/DRColorPicker.h index 61e39fb..f2b0ee2 100644 --- a/Pod/Classes/DRColorPicker.h +++ b/Pod/Classes/DRColorPicker.h @@ -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; diff --git a/Pod/Classes/DRColorPicker.m b/Pod/Classes/DRColorPicker.m index f045164..db403b1 100644 --- a/Pod/Classes/DRColorPicker.m +++ b/Pod/Classes/DRColorPicker.m @@ -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]; @@ -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); diff --git a/Pod/Classes/DRColorPickerHomeView.h b/Pod/Classes/DRColorPickerHomeView.h index 8146128..6434078 100644 --- a/Pod/Classes/DRColorPickerHomeView.h +++ b/Pod/Classes/DRColorPickerHomeView.h @@ -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* customColors; @end diff --git a/Pod/Classes/DRColorPickerHomeView.m b/Pod/Classes/DRColorPickerHomeView.m index 59a099c..dcc1922 100644 --- a/Pod/Classes/DRColorPickerHomeView.m +++ b/Pod/Classes/DRColorPickerHomeView.m @@ -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); @@ -111,7 +111,7 @@ - (void) setFrame:(CGRect)f - (void) doLayout { - if (self.standardColors == nil) + if (self.standardColorsGridView == nil) { return; } @@ -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++) @@ -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 diff --git a/Pod/Classes/DRColorPickerHomeViewController.h b/Pod/Classes/DRColorPickerHomeViewController.h index b3854de..385b5ce 100644 --- a/Pod/Classes/DRColorPickerHomeViewController.h +++ b/Pod/Classes/DRColorPickerHomeViewController.h @@ -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* customColors; + @end diff --git a/Pod/Classes/DRColorPickerHomeViewController.m b/Pod/Classes/DRColorPickerHomeViewController.m index b5bf734..5e599ae 100644 --- a/Pod/Classes/DRColorPickerHomeViewController.m +++ b/Pod/Classes/DRColorPickerHomeViewController.m @@ -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; @@ -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]; @@ -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 *)customColors +{ + _customColors = customColors; + self.homeView.customColors = customColors; +} + - (UIImage*) addToFavoritesImage { return self.homeView.addToFavoritesImage; diff --git a/Pod/Classes/DRColorPickerViewController.h b/Pod/Classes/DRColorPickerViewController.h index 4abd953..1e01faf 100644 --- a/Pod/Classes/DRColorPickerViewController.h +++ b/Pod/Classes/DRColorPickerViewController.h @@ -37,5 +37,12 @@ + (DRColorPickerViewController*) newColorPickerWithColor:(DRColorPickerColor*)color; @property (nonatomic, strong, readonly) DRColorPickerHomeViewController* rootViewController; +// Custom colors for Home View +@property (nonatomic, strong) NSArray* customColors; +// Convenience property for customColors property +@property (nonatomic, strong) NSArray* customUIColors; + +@property (nonatomic, strong) NSString *cancelButtonTitle; +@property (nonatomic, strong) NSString *doneButtonTitle; @end diff --git a/Pod/Classes/DRColorPickerViewController.m b/Pod/Classes/DRColorPickerViewController.m index 12f5b90..e5e073e 100644 --- a/Pod/Classes/DRColorPickerViewController.m +++ b/Pod/Classes/DRColorPickerViewController.m @@ -67,6 +67,36 @@ - (DRColorPickerHomeViewController*) rootViewController return (DRColorPickerHomeViewController*)self.viewControllers[0]; } +- (void)setCustomColors:(NSArray *)customColors +{ + _customColors = customColors; + self.rootViewController.customColors = customColors; +} + +- (void)setCustomUIColors:(NSArray *)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;