Implement numberOfLines support in React flat rendering.

Summary: Support numberOfLines in React with flat rendering.

Differential Revision: D2746855
This commit is contained in:
Ahmed El-Helw 2015-12-21 16:27:28 -08:00
parent c5d0cb70d7
commit 12023b7953

View File

@ -20,6 +20,7 @@ import android.text.TextUtils;
import com.facebook.csslayout.CSSNode; import com.facebook.csslayout.CSSNode;
import com.facebook.csslayout.MeasureOutput; import com.facebook.csslayout.MeasureOutput;
import com.facebook.fbui.widget.text.staticlayouthelper.StaticLayoutHelper;
import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactProp; import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.ViewDefaults; import com.facebook.react.uimanager.ViewDefaults;
@ -45,6 +46,7 @@ import com.facebook.react.uimanager.ViewProps;
private @Nullable BoringLayout.Metrics mBoringLayoutMetrics; private @Nullable BoringLayout.Metrics mBoringLayoutMetrics;
private float mSpacingMult = 1.0f; private float mSpacingMult = 1.0f;
private float mSpacingAdd = 0.0f; private float mSpacingAdd = 0.0f;
private int mNumberOfLines = Integer.MAX_VALUE;
public RCTText() { public RCTText() {
setMeasureFunction(this); setMeasureFunction(this);
@ -105,14 +107,20 @@ import com.facebook.react.uimanager.ViewProps;
int maximumWidth = Float.isNaN(width) ? Integer.MAX_VALUE : (int) width; int maximumWidth = Float.isNaN(width) ? Integer.MAX_VALUE : (int) width;
// at this point we need to create a StaticLayout to measure the text // at this point we need to create a StaticLayout to measure the text
StaticLayout layout = new StaticLayout( StaticLayout layout = StaticLayoutHelper.make(
text, text,
0,
text.length(),
PAINT, PAINT,
maximumWidth, maximumWidth,
Layout.Alignment.ALIGN_NORMAL, Layout.Alignment.ALIGN_NORMAL,
mSpacingMult, mSpacingMult,
mSpacingAdd, mSpacingAdd,
INCLUDE_PADDING); INCLUDE_PADDING,
TextUtils.TruncateAt.END,
maximumWidth,
mNumberOfLines,
false);
// determine how wide we actually are // determine how wide we actually are
float maxLineWidth = 0; float maxLineWidth = 0;
@ -196,6 +204,12 @@ import com.facebook.react.uimanager.ViewProps;
notifyChanged(true); notifyChanged(true);
} }
@ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = Integer.MAX_VALUE)
public void setNumberOfLines(int numberOfLines) {
mNumberOfLines = numberOfLines;
notifyChanged(true);
}
@Override @Override
/* package */ void updateNodeRegion(float left, float top, float right, float bottom) { /* package */ void updateNodeRegion(float left, float top, float right, float bottom) {
if (mDrawCommand == null) { if (mDrawCommand == null) {