From e0969c30cda1f3b5d4d19f77c52170bf1761739d Mon Sep 17 00:00:00 2001 From: toya-niibori Date: Mon, 1 Apr 2024 12:01:41 +0900 Subject: [PATCH] make reverseContentLayout externally modifiable --- .../TOCropViewController/TOCropViewController.h | 8 ++++++++ .../TOCropViewController/TOCropViewController.m | 10 ++++++++++ .../TOCropViewController/Views/TOCropToolbar.h | 3 +++ .../TOCropViewController/Views/TOCropToolbar.m | 14 ++++++++++---- .../TOCropViewControllerExample/ViewController.m | 4 +++- Swift/CropViewController/CropViewController.swift | 13 ++++++++++++- .../CropViewControllerExample/ViewController.swift | 3 +++ 7 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Objective-C/TOCropViewController/TOCropViewController.h b/Objective-C/TOCropViewController/TOCropViewController.h index 044b9646..854c6952 100755 --- a/Objective-C/TOCropViewController/TOCropViewController.h +++ b/Objective-C/TOCropViewController/TOCropViewController.h @@ -285,6 +285,14 @@ */ @property (nonatomic, assign) BOOL cancelButtonHidden; +/** + When enabled, the toolbar is displayed in RTL layout. + + Default is NO. + */ +@property (nonatomic, assign) BOOL reverseContentLayout +; + /** If `showActivitySheetOnDone` is true, then these activity items will be supplied to that UIActivityViewController in addition to the diff --git a/Objective-C/TOCropViewController/TOCropViewController.m b/Objective-C/TOCropViewController/TOCropViewController.m index ce5c7182..3c63e357 100755 --- a/Objective-C/TOCropViewController/TOCropViewController.m +++ b/Objective-C/TOCropViewController/TOCropViewController.m @@ -1186,6 +1186,16 @@ - (BOOL)cancelButtonHidden return self.toolbar.cancelButtonHidden; } +- (BOOL)reverseContentLayout +{ + return self.toolbar.reverseContentLayout; +} +- (void)setReverseContentLayout:(BOOL)reverseContentLayout +{ + + self.toolbar.reverseContentLayout = reverseContentLayout; +} + - (void)setResetAspectRatioEnabled:(BOOL)resetAspectRatioEnabled { self.cropView.resetAspectRatioEnabled = resetAspectRatioEnabled; diff --git a/Objective-C/TOCropViewController/Views/TOCropToolbar.h b/Objective-C/TOCropViewController/Views/TOCropToolbar.h index 55411dcf..d1474172 100644 --- a/Objective-C/TOCropViewController/Views/TOCropToolbar.h +++ b/Objective-C/TOCropViewController/Views/TOCropToolbar.h @@ -78,6 +78,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign) BOOL doneButtonHidden; @property (nonatomic, assign) BOOL cancelButtonHidden; +/* For languages like Arabic where they natively present content flipped from English */ +@property (nonatomic, assign) BOOL reverseContentLayout; + /* Enable the reset button */ @property (nonatomic, assign) BOOL resetButtonEnabled; diff --git a/Objective-C/TOCropViewController/Views/TOCropToolbar.m b/Objective-C/TOCropViewController/Views/TOCropToolbar.m index abeb6051..1bac8d27 100644 --- a/Objective-C/TOCropViewController/Views/TOCropToolbar.m +++ b/Objective-C/TOCropViewController/Views/TOCropToolbar.m @@ -39,8 +39,6 @@ @interface TOCropToolbar() @property (nonatomic, strong) UIButton *rotateButton; // defaults to counterclockwise button for legacy compatibility -@property (nonatomic, assign) BOOL reverseContentLayout; // For languages like Arabic where they natively present content flipped from English - @end @implementation TOCropToolbar @@ -61,10 +59,10 @@ - (void)setup { // On iOS 9, we can use the new layout features to determine whether we're in an 'Arabic' style language mode if (@available(iOS 9.0, *)) { - self.reverseContentLayout = ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:self.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft); + _reverseContentLayout = ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:self.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft); } else { - self.reverseContentLayout = [[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@"ar"]; + _reverseContentLayout = [[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@"ar"]; } // Get the resource bundle depending on the framework/dependency manager we're using @@ -343,6 +341,14 @@ - (CGRect)clampButtonFrame return self.clampButton.frame; } +- (void)setReverseContentLayout:(BOOL)reverseContentLayout { + if (_reverseContentLayout == reverseContentLayout) + return; + + _reverseContentLayout = reverseContentLayout; + [self setNeedsLayout]; +} + - (void)setClampButtonHidden:(BOOL)clampButtonHidden { if (_clampButtonHidden == clampButtonHidden) return; diff --git a/Objective-C/TOCropViewControllerExample/ViewController.m b/Objective-C/TOCropViewControllerExample/ViewController.m index 547ce7e5..d9666a24 100644 --- a/Objective-C/TOCropViewControllerExample/ViewController.m +++ b/Objective-C/TOCropViewControllerExample/ViewController.m @@ -52,7 +52,9 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking //cropController.rotateButtonsHidden = YES; //cropController.rotateClockwiseButtonHidden = NO; - + + //cropController.reverseContentLayout = YES; + //cropController.doneButtonTitle = @"Title"; //cropController.cancelButtonTitle = @"Title"; diff --git a/Swift/CropViewController/CropViewController.swift b/Swift/CropViewController/CropViewController.swift index 58b80a0a..c8a98136 100644 --- a/Swift/CropViewController/CropViewController.swift +++ b/Swift/CropViewController/CropViewController.swift @@ -435,7 +435,18 @@ open class CropViewController: UIViewController, TOCropViewControllerDelegate { set { toCropViewController.cancelButtonColor = newValue } get { return toCropViewController.cancelButtonColor } } - + + /** + A computed property to get or set the reverse layout on toolbar. + By setting this property, you can control the direction in which the toolbar is laid out. + + Default is NO. + */ + public var reverseContentLayout: Bool { + set { toCropViewController.reverseContentLayout = newValue } + get { toCropViewController.reverseContentLayout } + } + /** This class internally manages and abstracts access to a `TOCropViewController` instance :nodoc: diff --git a/Swift/CropViewControllerExample/ViewController.swift b/Swift/CropViewControllerExample/ViewController.swift index 027b0a80..6bcba7e2 100644 --- a/Swift/CropViewControllerExample/ViewController.swift +++ b/Swift/CropViewControllerExample/ViewController.swift @@ -55,6 +55,9 @@ class ViewController: UIViewController, CropViewControllerDelegate, UIImagePicke // cropController.doneButtonColor = UIColor.red // cropController.cancelButtonColor = UIColor.green + // Change toolbar layout direction + // cropController.toolbar.reverseContentLayout = true + self.image = image //If profile picture, push onto the same navigation stack