From 717c3f097756176efc3beea1fdf15d2d0727b729 Mon Sep 17 00:00:00 2001 From: YoungSeung Seo Date: Thu, 23 Nov 2023 22:52:26 +0900 Subject: [PATCH] add: Added text centering options --- .../reactnativesnackbar/SnackbarModule.java | 15 +++++++++++++++ example/src/App.js | 6 ++++++ ios/RNSnackBarView.m | 10 ++++++++++ src/index.js | 7 +++++++ 4 files changed, 38 insertions(+) diff --git a/android/src/main/java/com/azendoo/reactnativesnackbar/SnackbarModule.java b/android/src/main/java/com/azendoo/reactnativesnackbar/SnackbarModule.java index a2f08c8..a297652 100644 --- a/android/src/main/java/com/azendoo/reactnativesnackbar/SnackbarModule.java +++ b/android/src/main/java/com/azendoo/reactnativesnackbar/SnackbarModule.java @@ -6,6 +6,7 @@ import android.content.Context; import android.util.DisplayMetrics; import android.util.Log; +import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @@ -138,6 +139,7 @@ private void displaySnackbar(View view, ReadableMap options, final Callback call int duration = getOptionValue(options, "duration", Snackbar.LENGTH_SHORT); int numberOfLines = getOptionValue(options, "numberOfLines", 2); int textColor = getOptionValue(options, "textColor", Color.WHITE); + boolean textAlignCenter = getOptionValue(options, "textAlignCenter", false); boolean rtl = getOptionValue(options, "rtl", false); int marginBottom = getOptionValue(options, "marginBottom", 0); String fontFamily = getOptionValue(options, "fontFamily", null); @@ -179,6 +181,19 @@ private void displaySnackbar(View view, ReadableMap options, final Callback call TextView snackbarText = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text); snackbarText.setMaxLines(numberOfLines); snackbarText.setTextColor(textColor); + if (textAlignCenter) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ + snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); + } else { + snackbarText.setGravity(Gravity.CENTER_HORIZONTAL); + } + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ + snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); + } else { + snackbarText.setGravity(Gravity.START); + } + } if (font != null) { snackbarText.setTypeface(font); diff --git a/example/src/App.js b/example/src/App.js index a6da610..87b2571 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -30,6 +30,12 @@ class Example extends Component { Simple Snackbar + Snackbar.show({ text: 'Hello, World!', textAlignCenter: true })} + > + Align text center + + Snackbar.show({ text: 'Hello, World! How are you doing today? Enjoying the sun?! This should wrap to two lines.', diff --git a/ios/RNSnackBarView.m b/ios/RNSnackBarView.m index 34071ec..7575aeb 100644 --- a/ios/RNSnackBarView.m +++ b/ios/RNSnackBarView.m @@ -33,6 +33,7 @@ @interface RNSnackBarView () { @property(nonatomic, strong) UIColor *actionTextColor; @property(nonatomic, strong) NSNumber *marginBottom; @property(nonatomic, strong) NSArray *verticalPaddingConstraints; +@property(nonatomic) BOOL textAlignCenter; @property(nonatomic, strong) void (^pendingCallback)(); @property(nonatomic, strong) void (^callback)(); @@ -158,6 +159,14 @@ - (void)setNumberOfLines:(int *)numberOfLines { textLabel.numberOfLines = numberOfLines; } +- (void)setTextAlignCenter:(BOOL)textAlignCenter { + if (textAlignCenter == YES) { + textLabel.textAlignment = NSTextAlignmentCenter; + } else { + textLabel.textAlignment = NSTextAlignmentLeft; + } +} + - (void)setActionText:(NSString *)actionText { [actionButton setTitle:actionText forState:UIControlStateNormal]; } @@ -297,6 +306,7 @@ - (void)show { self.textColor = textColor ? [RCTConvert UIColor:textColor] : [UIColor whiteColor]; self.text = _pendingOptions[@"text"]; + self.textAlignCenter = [_pendingOptions[@"textAlignCenter"] boolValue]; self.callback = _pendingCallback; NSDictionary *action = _pendingOptions[@"action"]; diff --git a/src/index.js b/src/index.js index 04a21c9..88b782f 100644 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,11 @@ type SnackBarOptions = { */ numberOfLines?: number, + /** + * Align text center + */ + textAlignCenter?: boolean, + /** * Length of time the Snackbar stays on screen. * Must be one of Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG, or Snackbar.LENGTH_INDEFINITE. @@ -156,6 +161,7 @@ const SnackBar: ISnackBar = { delete options.color; const textColor = textColorRaw && processColor(textColorRaw); const backgroundColor = options.backgroundColor && processColor(options.backgroundColor); + const textAlignCenter = options.textAlignCenter || false; const action = options.action || {}; @@ -175,6 +181,7 @@ const SnackBar: ISnackBar = { textColor, numberOfLines, backgroundColor, + textAlignCenter, action: options.action ? { ...action, text: actionText,