Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: Added text centering options #206

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class Example extends Component {
<Text style={styles.button}>Simple Snackbar</Text>
</TouchableOpacity>

<TouchableOpacity
onPress={() => Snackbar.show({ text: 'Hello, World!', textAlignCenter: true })}
>
<Text style={styles.button}>Align text center</Text>
</TouchableOpacity>

<TouchableOpacity
onPress={() => Snackbar.show({
text: 'Hello, World! How are you doing today? Enjoying the sun?! This should wrap to two lines.',
Expand Down
10 changes: 10 additions & 0 deletions ios/RNSnackBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ @interface RNSnackBarView () {
@property(nonatomic, strong) UIColor *actionTextColor;
@property(nonatomic, strong) NSNumber *marginBottom;
@property(nonatomic, strong) NSArray<NSLayoutConstraint *> *verticalPaddingConstraints;
@property(nonatomic) BOOL textAlignCenter;
@property(nonatomic, strong) void (^pendingCallback)();
@property(nonatomic, strong) void (^callback)();

Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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"];
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 || {};

Expand All @@ -175,6 +181,7 @@ const SnackBar: ISnackBar = {
textColor,
numberOfLines,
backgroundColor,
textAlignCenter,
action: options.action ? {
...action,
text: actionText,
Expand Down