Skip to content

Commit

Permalink
[iOS only] Feat: Add the way to customize iOS cropper toolbar (ivpusi…
Browse files Browse the repository at this point in the history
…c#1923)

* feat: update `index.d.ts` definitions to include new params for colors customization

* feat: add the way to customize iOS toolbar buttons with HEX colors
  • Loading branch information
sctfcm committed Oct 9, 2023
1 parent 74d54b8 commit 9bcc249
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,29 @@ declare module "react-native-image-crop-picker" {
*/
cropperCancelText?: string;

/**
* Cancel button color. HEX-like string color.
*
* @example '#ff00ee'
* @platform iOS only
*/
cropperCancelColor?: string;

/**
* Choose button text.
*
* @default 'Choose'
*/
cropperChooseText?: string;

/**
* Choose button color. HEX-like string color.
*
* @example '#EE00DD'
* @platform iOS only
*/
cropperChooseColor?: string;

/**
* Whether to show the 3x3 grid on top of the image during cropping.
*
Expand Down
19 changes: 19 additions & 0 deletions ios/src/ImageCropPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,15 @@ + (NSDictionary *)cgRectToDictionary:(CGRect)rect {
};
}

// Assumes input like "#00FF00" (#RRGGBB).
+ (UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}

#pragma mark - TOCCropViewController Implementation
- (void)cropImage:(UIImage *)image {
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -891,6 +900,16 @@ - (void)cropImage:(UIImage *)image {
cropVC.title = [[self options] objectForKey:@"cropperToolbarTitle"];
cropVC.delegate = self;

NSString* rawDoneButtonColor = [self.options objectForKey:@"cropperChooseColor"];
NSString* rawCancelButtonColor = [self.options objectForKey:@"cropperCancelColor"];

if (rawDoneButtonColor) {
cropVC.doneButtonColor = [ImageCropPicker colorFromHexString: rawDoneButtonColor];
}
if (rawCancelButtonColor) {
cropVC.cancelButtonColor = [ImageCropPicker colorFromHexString: rawCancelButtonColor];
}

cropVC.doneButtonTitle = [self.options objectForKey:@"cropperChooseText"];
cropVC.cancelButtonTitle = [self.options objectForKey:@"cropperCancelText"];

Expand Down

0 comments on commit 9bcc249

Please sign in to comment.