Skip to content

Commit f5f3ff1

Browse files
authored
Merge pull request #206 from areemadev/main
add: Added text centering options
2 parents 1f956f1 + 717c3f0 commit f5f3ff1

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

android/src/main/java/com/azendoo/reactnativesnackbar/SnackbarModule.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Context;
77
import android.util.DisplayMetrics;
88
import android.util.Log;
9+
import android.view.Gravity;
910
import android.view.View;
1011
import android.view.ViewGroup;
1112
import android.widget.TextView;
@@ -138,6 +139,7 @@ private void displaySnackbar(View view, ReadableMap options, final Callback call
138139
int duration = getOptionValue(options, "duration", Snackbar.LENGTH_SHORT);
139140
int numberOfLines = getOptionValue(options, "numberOfLines", 2);
140141
int textColor = getOptionValue(options, "textColor", Color.WHITE);
142+
boolean textAlignCenter = getOptionValue(options, "textAlignCenter", false);
141143
boolean rtl = getOptionValue(options, "rtl", false);
142144
int marginBottom = getOptionValue(options, "marginBottom", 0);
143145
String fontFamily = getOptionValue(options, "fontFamily", null);
@@ -179,6 +181,19 @@ private void displaySnackbar(View view, ReadableMap options, final Callback call
179181
TextView snackbarText = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
180182
snackbarText.setMaxLines(numberOfLines);
181183
snackbarText.setTextColor(textColor);
184+
if (textAlignCenter) {
185+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
186+
snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
187+
} else {
188+
snackbarText.setGravity(Gravity.CENTER_HORIZONTAL);
189+
}
190+
} else {
191+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
192+
snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
193+
} else {
194+
snackbarText.setGravity(Gravity.START);
195+
}
196+
}
182197

183198
if (font != null) {
184199
snackbarText.setTypeface(font);

example/src/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class Example extends Component {
3030
<Text style={styles.button}>Simple Snackbar</Text>
3131
</TouchableOpacity>
3232

33+
<TouchableOpacity
34+
onPress={() => Snackbar.show({ text: 'Hello, World!', textAlignCenter: true })}
35+
>
36+
<Text style={styles.button}>Align text center</Text>
37+
</TouchableOpacity>
38+
3339
<TouchableOpacity
3440
onPress={() => Snackbar.show({
3541
text: 'Hello, World! How are you doing today? Enjoying the sun?! This should wrap to two lines.',

ios/RNSnackBarView.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ @interface RNSnackBarView () {
3333
@property(nonatomic, strong) UIColor *actionTextColor;
3434
@property(nonatomic, strong) NSNumber *marginBottom;
3535
@property(nonatomic, strong) NSArray<NSLayoutConstraint *> *verticalPaddingConstraints;
36+
@property(nonatomic) BOOL textAlignCenter;
3637
@property(nonatomic, strong) void (^pendingCallback)();
3738
@property(nonatomic, strong) void (^callback)();
3839

@@ -158,6 +159,14 @@ - (void)setNumberOfLines:(int *)numberOfLines {
158159
textLabel.numberOfLines = numberOfLines;
159160
}
160161

162+
- (void)setTextAlignCenter:(BOOL)textAlignCenter {
163+
if (textAlignCenter == YES) {
164+
textLabel.textAlignment = NSTextAlignmentCenter;
165+
} else {
166+
textLabel.textAlignment = NSTextAlignmentLeft;
167+
}
168+
}
169+
161170
- (void)setActionText:(NSString *)actionText {
162171
[actionButton setTitle:actionText forState:UIControlStateNormal];
163172
}
@@ -297,6 +306,7 @@ - (void)show {
297306
self.textColor = textColor ? [RCTConvert UIColor:textColor] : [UIColor whiteColor];
298307

299308
self.text = _pendingOptions[@"text"];
309+
self.textAlignCenter = [_pendingOptions[@"textAlignCenter"] boolValue];
300310
self.callback = _pendingCallback;
301311

302312
NSDictionary *action = _pendingOptions[@"action"];

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type SnackBarOptions = {
3838
*/
3939
numberOfLines?: number,
4040

41+
/**
42+
* Align text center
43+
*/
44+
textAlignCenter?: boolean,
45+
4146
/**
4247
* Length of time the Snackbar stays on screen.
4348
* Must be one of Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG, or Snackbar.LENGTH_INDEFINITE.
@@ -156,6 +161,7 @@ const SnackBar: ISnackBar = {
156161
delete options.color;
157162
const textColor = textColorRaw && processColor(textColorRaw);
158163
const backgroundColor = options.backgroundColor && processColor(options.backgroundColor);
164+
const textAlignCenter = options.textAlignCenter || false;
159165

160166
const action = options.action || {};
161167

@@ -175,6 +181,7 @@ const SnackBar: ISnackBar = {
175181
textColor,
176182
numberOfLines,
177183
backgroundColor,
184+
textAlignCenter,
178185
action: options.action ? {
179186
...action,
180187
text: actionText,

0 commit comments

Comments
 (0)