Add <Text> shadow support

Summary:
Add three new TextStylePropTypes for \<Text>
- textShadowOffset
- textShadowRadius
- textShadowColor
Closes https://github.com/facebook/react-native/pull/4975

Reviewed By: svcscm

Differential Revision: D2796278

Pulled By: nicklockwood

fb-gh-sync-id: f8c3fa210e664428b029b9fba8eca4a8eb81c08d
This commit is contained in:
Kudo Chien
2016-01-01 09:32:59 -08:00
committed by facebook-github-bot-9
parent 718cd7953f
commit 4972cabaa5
9 changed files with 126 additions and 7 deletions

View File

@@ -32,6 +32,7 @@ import com.facebook.csslayout.CSSConstants;
import com.facebook.csslayout.CSSNode;
import com.facebook.csslayout.MeasureOutput;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.LayoutShadowNode;
@@ -63,6 +64,11 @@ public class ReactTextShadowNode extends LayoutShadowNode {
@VisibleForTesting
public static final String PROP_TEXT = "text";
public static final String PROP_SHADOW_OFFSET = "textShadowOffset";
public static final String PROP_SHADOW_RADIUS = "textShadowRadius";
public static final String PROP_SHADOW_COLOR = "textShadowColor";
public static final int DEFAULT_TEXT_SHADOW_COLOR = 0x55000000;
private static final TextPaint sTextPaintInstance = new TextPaint();
static {
@@ -114,8 +120,7 @@ public class ReactTextShadowNode extends LayoutShadowNode {
ops.add(new SetSpanOperation(start, end, new ForegroundColorSpan(textCSSNode.mColor)));
}
if (textCSSNode.mIsBackgroundColorSet) {
ops.add(
new SetSpanOperation(
ops.add(new SetSpanOperation(
start,
end,
new BackgroundColorSpan(textCSSNode.mBackgroundColor)));
@@ -135,6 +140,16 @@ public class ReactTextShadowNode extends LayoutShadowNode {
textCSSNode.mFontFamily,
textCSSNode.getThemedContext().getAssets())));
}
if (textCSSNode.mTextShadowOffsetDx != 0 || textCSSNode.mTextShadowOffsetDy != 0) {
ops.add(new SetSpanOperation(
start,
end,
new ShadowStyleSpan(
textCSSNode.mTextShadowOffsetDx,
textCSSNode.mTextShadowOffsetDy,
textCSSNode.mTextShadowRadius,
textCSSNode.mTextShadowColor)));
}
ops.add(new SetSpanOperation(start, end, new ReactTagSpan(textCSSNode.getReactTag())));
}
}
@@ -279,6 +294,11 @@ public class ReactTextShadowNode extends LayoutShadowNode {
protected int mNumberOfLines = UNSET;
protected int mFontSize = UNSET;
private float mTextShadowOffsetDx = 0;
private float mTextShadowOffsetDy = 0;
private float mTextShadowRadius = 1;
private int mTextShadowColor = DEFAULT_TEXT_SHADOW_COLOR;
/**
* mFontStyle can be {@link Typeface#NORMAL} or {@link Typeface#ITALIC}.
* mFontWeight can be {@link Typeface#NORMAL} or {@link Typeface#BOLD}.
@@ -413,6 +433,34 @@ public class ReactTextShadowNode extends LayoutShadowNode {
}
}
@ReactProp(name = PROP_SHADOW_OFFSET)
public void setTextShadowOffset(ReadableMap offsetMap) {
if (offsetMap == null) {
mTextShadowOffsetDx = 0;
mTextShadowOffsetDy = 0;
} else {
mTextShadowOffsetDx = PixelUtil.toPixelFromDIP(offsetMap.getDouble("width"));
mTextShadowOffsetDy = PixelUtil.toPixelFromDIP(offsetMap.getDouble("height"));
}
markUpdated();
}
@ReactProp(name = PROP_SHADOW_RADIUS, defaultInt = 1)
public void setTextShadowRadius(float textShadowRadius) {
if (textShadowRadius != mTextShadowRadius) {
mTextShadowRadius = textShadowRadius;
markUpdated();
}
}
@ReactProp(name = PROP_SHADOW_COLOR, defaultInt = DEFAULT_TEXT_SHADOW_COLOR, customType = "Color")
public void setTextShadowColor(int textShadowColor) {
if (textShadowColor != mTextShadowColor) {
mTextShadowColor = textShadowColor;
markUpdated();
}
}
@Override
public boolean isVirtualAnchor() {
return !mIsVirtual;

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.views.text;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
public class ShadowStyleSpan extends CharacterStyle {
private final float mDx, mDy, mRadius;
private final int mColor;
public ShadowStyleSpan(float dx, float dy, float radius, int color) {
mDx = dx;
mDy = dy;
mRadius = radius;
mColor = color;
}
@Override
public void updateDrawState(TextPaint textPaint) {
textPaint.setShadowLayer(mRadius, mDx, mDy, mColor);
}
}