mirror of
https://github.com/status-im/react-native.git
synced 2025-01-14 19:44:13 +00:00
Add missing textAlignVertical support on android textview
Summary:Add missing implementation of `textAlignVertical` style prop for `Text`-component.
According to the [docs](https://facebook.github.io/react-native/docs/text.html#style) it should be implemented but was only added for `TextInput` in f453e14c8f
.
Closes https://github.com/facebook/react-native/pull/6105
Differential Revision: D3029783
Pulled By: dmmiller
fb-gh-sync-id: 29bcd50a7280c890707f811bcfc7a3d5c5be5142
shipit-source-id: 29bcd50a7280c890707f811bcfc7a3d5c5be5142
This commit is contained in:
parent
8c2e531542
commit
d20bde3f2e
@ -13,6 +13,7 @@ import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.Layout;
|
||||
import android.text.Spanned;
|
||||
import android.view.Gravity;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.facebook.react.uimanager.ReactCompoundView;
|
||||
@ -20,9 +21,14 @@ import com.facebook.react.uimanager.ReactCompoundView;
|
||||
public class ReactTextView extends TextView implements ReactCompoundView {
|
||||
|
||||
private boolean mContainsImages;
|
||||
private int mDefaultGravityHorizontal;
|
||||
private int mDefaultGravityVertical;
|
||||
|
||||
public ReactTextView(Context context) {
|
||||
super(context);
|
||||
mDefaultGravityHorizontal =
|
||||
getGravity() & (Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK);
|
||||
mDefaultGravityVertical = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
|
||||
}
|
||||
|
||||
public void setText(ReactTextUpdate update) {
|
||||
@ -150,4 +156,20 @@ public class ReactTextView extends TextView implements ReactCompoundView {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* package */ void setGravityHorizontal(int gravityHorizontal) {
|
||||
if (gravityHorizontal == 0) {
|
||||
gravityHorizontal = mDefaultGravityHorizontal;
|
||||
}
|
||||
setGravity(
|
||||
(getGravity() & ~Gravity.HORIZONTAL_GRAVITY_MASK &
|
||||
~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravityHorizontal);
|
||||
}
|
||||
|
||||
/* package */ void setGravityVertical(int gravityVertical) {
|
||||
if (gravityVertical == 0) {
|
||||
gravityVertical = mDefaultGravityVertical;
|
||||
}
|
||||
setGravity((getGravity() & ~Gravity.VERTICAL_GRAVITY_MASK) | gravityVertical);
|
||||
}
|
||||
}
|
||||
|
@ -58,18 +58,33 @@ public class ReactTextViewManager extends BaseViewManager<ReactTextView, ReactTe
|
||||
@ReactProp(name = ViewProps.TEXT_ALIGN)
|
||||
public void setTextAlign(ReactTextView view, @Nullable String textAlign) {
|
||||
if (textAlign == null || "auto".equals(textAlign)) {
|
||||
view.setGravity(Gravity.NO_GRAVITY);
|
||||
view.setGravityHorizontal(Gravity.NO_GRAVITY);
|
||||
} else if ("left".equals(textAlign)) {
|
||||
view.setGravity(Gravity.LEFT);
|
||||
view.setGravityHorizontal(Gravity.LEFT);
|
||||
} else if ("right".equals(textAlign)) {
|
||||
view.setGravity(Gravity.RIGHT);
|
||||
view.setGravityHorizontal(Gravity.RIGHT);
|
||||
} else if ("center".equals(textAlign)) {
|
||||
view.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
view.setGravityHorizontal(Gravity.CENTER_HORIZONTAL);
|
||||
} else {
|
||||
throw new JSApplicationIllegalArgumentException("Invalid textAlign: " + textAlign);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactProp(name = ViewProps.TEXT_ALIGN_VERTICAL)
|
||||
public void setTextAlignVertical(ReactTextView view, @Nullable String textAlignVertical) {
|
||||
if (textAlignVertical == null || "auto".equals(textAlignVertical)) {
|
||||
view.setGravityVertical(Gravity.NO_GRAVITY);
|
||||
} else if ("top".equals(textAlignVertical)) {
|
||||
view.setGravityVertical(Gravity.TOP);
|
||||
} else if ("bottom".equals(textAlignVertical)) {
|
||||
view.setGravityVertical(Gravity.BOTTOM);
|
||||
} else if ("center".equals(textAlignVertical)) {
|
||||
view.setGravityVertical(Gravity.CENTER_VERTICAL);
|
||||
} else {
|
||||
throw new JSApplicationIllegalArgumentException("Invalid textAlignVertical: " + textAlignVertical);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactProp(name = ViewProps.LINE_HEIGHT, defaultFloat = Float.NaN)
|
||||
public void setLineHeight(ReactTextView view, float lineHeight) {
|
||||
if (Float.isNaN(lineHeight)) { // NaN will be used if property gets reset
|
||||
|
@ -289,26 +289,45 @@ public class ReactTextInputPropertyTest {
|
||||
@Test
|
||||
public void testTextAlign() {
|
||||
ReactEditText view = mManager.createViewInstance(mThemedContext);
|
||||
int gravity = view.getGravity();
|
||||
assertThat(view.getGravity() & Gravity.BOTTOM).isNotEqualTo(Gravity.BOTTOM);
|
||||
int defaultGravity = view.getGravity();
|
||||
int defaultHorizontalGravity = defaultGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
|
||||
int defaultVerticalGravity = defaultGravity & Gravity.VERTICAL_GRAVITY_MASK;
|
||||
|
||||
mManager.updateProperties(view, buildStyles("textAlignVertical", "bottom"));
|
||||
assertThat(view.getGravity() & Gravity.BOTTOM).isEqualTo(Gravity.BOTTOM);
|
||||
|
||||
mManager.updateProperties(
|
||||
view,
|
||||
buildStyles("textAlign", "right", "textAlignVertical", "top"));
|
||||
assertThat(view.getGravity() & Gravity.BOTTOM).isNotEqualTo(Gravity.BOTTOM);
|
||||
assertThat(view.getGravity() & (Gravity.RIGHT | Gravity.TOP))
|
||||
.isEqualTo(Gravity.RIGHT | Gravity.TOP);
|
||||
|
||||
mManager.updateProperties(
|
||||
view,
|
||||
buildStyles("textAlignVertical", null));
|
||||
assertThat(view.getGravity() & Gravity.RIGHT).isEqualTo(Gravity.RIGHT);
|
||||
assertThat(view.getGravity() & Gravity.TOP).isNotEqualTo(Gravity.TOP);
|
||||
// Theme
|
||||
assertThat(view.getGravity()).isNotEqualTo(Gravity.NO_GRAVITY);
|
||||
|
||||
// TextAlign
|
||||
mManager.updateProperties(view, buildStyles("textAlign", "left"));
|
||||
assertThat(view.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK).isEqualTo(Gravity.LEFT);
|
||||
mManager.updateProperties(view, buildStyles("textAlign", "right"));
|
||||
assertThat(view.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK).isEqualTo(Gravity.RIGHT);
|
||||
mManager.updateProperties(view, buildStyles("textAlign", "center"));
|
||||
assertThat(view.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK).isEqualTo(Gravity.CENTER_HORIZONTAL);
|
||||
mManager.updateProperties(view, buildStyles("textAlign", null));
|
||||
assertThat(view.getGravity()).isEqualTo(gravity);
|
||||
assertThat(view.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK).isEqualTo(defaultHorizontalGravity);
|
||||
|
||||
// TextAlignVertical
|
||||
mManager.updateProperties(view, buildStyles("textAlignVertical", "top"));
|
||||
assertThat(view.getGravity() & Gravity.VERTICAL_GRAVITY_MASK).isEqualTo(Gravity.TOP);
|
||||
mManager.updateProperties(view, buildStyles("textAlignVertical", "bottom"));
|
||||
assertThat(view.getGravity() & Gravity.VERTICAL_GRAVITY_MASK).isEqualTo(Gravity.BOTTOM);
|
||||
mManager.updateProperties(view, buildStyles("textAlignVertical", "center"));
|
||||
assertThat(view.getGravity() & Gravity.VERTICAL_GRAVITY_MASK).isEqualTo(Gravity.CENTER_VERTICAL);
|
||||
mManager.updateProperties(view, buildStyles("textAlignVertical", null));
|
||||
assertThat(view.getGravity() & Gravity.VERTICAL_GRAVITY_MASK).isEqualTo(defaultVerticalGravity);
|
||||
|
||||
// TextAlign + TextAlignVertical
|
||||
mManager.updateProperties(
|
||||
view,
|
||||
buildStyles("textAlign", "center", "textAlignVertical", "center"));
|
||||
assertThat(view.getGravity()).isEqualTo(Gravity.CENTER);
|
||||
mManager.updateProperties(
|
||||
view,
|
||||
buildStyles("textAlign", "right", "textAlignVertical", "bottom"));
|
||||
assertThat(view.getGravity()).isEqualTo(Gravity.RIGHT | Gravity.BOTTOM);
|
||||
mManager.updateProperties(
|
||||
view,
|
||||
buildStyles("textAlign", null, "textAlignVertical", null));
|
||||
assertThat(view.getGravity()).isEqualTo(defaultGravity);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user