React Native字体轮廓/ textShadow

Bri*_*ean 15 css css3 react-native

是否可以将一个大纲或textShadow添加到反应原生的字体中以实现类似这样的字体(带有黑色轮廓的白色字体):

在此输入图像描述

在Web上的CSS中,可以为字体添加文本阴影或轮廓,为文本提供字体后面的边框,如下所示:

h1 {
    color: yellow;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
Run Code Online (Sandbox Code Playgroud)
<h1>Hello World</h1>
Run Code Online (Sandbox Code Playgroud)

在本地做出反应是否有可能做类似的事情?

我从这个堆栈溢出帖子中获取了关于如何使用CSS:CSS字体边框的CCS片段示例

编辑:更新了问题,试图让它更清晰

Shi*_*nha 10

是的,可以通过以下属性:

textShadowColor color
textShadowOffset ReactPropTypes.shape( {width: ReactPropTypes.number, height: ReactPropTypes.number} )
textShadowRadius ReactPropTypes.number
Run Code Online (Sandbox Code Playgroud)

https://facebook.github.io/react-native/docs/text.html

实际完成的拉取请求:https: //github.com/facebook/react-native/pull/4975

  • React Native的textShadow*样式中途达到你想要的效果.问题是边框只在一个方向上渲染,而不是在文本周围. (4认同)

lub*_*b0v 9

在 ios 和 android 上至少有一种方法可以使它看起来像这样:

带有轮廓文本的 iPhone 模拟器屏幕截图

主意:

这个想法是在 Text 对象上使用多个阴影。我们可以通过用 View 包裹 Text 组件并使用不同的阴影多次克隆相同的 Text 对象来使它们使用不同的方向来实现。

执行:

这是包装器组件的代码:

import * as React from "react";
import { StyleSheet, View } from "react-native";
import { Children, cloneElement, isValidElement } from "react";

type Props = {
  children: any,
  color: string,
  stroke: number
}
const styles = StyleSheet.create({
  outline: {
    position: 'absolute'
  },
});

export class TextStroke extends React.Component<Props> {
  createClones = (w: number, h: number, color?: string) => {
    const { children } = this.props;
    return Children.map(children, child => {
      if (isValidElement(child)) {
        const currentProps = child.props as any;
        const currentStyle = currentProps ? (currentProps.style || {}) : {};

        const newProps = {
          ...currentProps,
          style: {
            ...currentStyle,
            textShadowOffset: {
              width: w,
              height: h
            },
            textShadowColor: color,
            textShadowRadius: 1
          }
        }
        return cloneElement(child, newProps)
      }
      return child;
    });
  }

  render() {
    const {color, stroke, children} = this.props;
    const strokeW = stroke;
    const top = this.createClones(0, -strokeW * 1.2, color);
    const topLeft = this.createClones(-strokeW, -strokeW, color);
    const topRight = this.createClones(strokeW, -strokeW, color);
    const right = this.createClones(strokeW, 0, color);
    const bottom = this.createClones(0, strokeW, color);
    const bottomLeft = this.createClones(-strokeW, strokeW, color);
    const bottomRight = this.createClones(strokeW, strokeW, color);
    const left = this.createClones(-strokeW * 1.2, 0, color);

    return (
      <View>
        <View style={ styles.outline }>{ left }</View>
        <View style={ styles.outline }>{ right }</View>
        <View style={ styles.outline }>{ bottom }</View>
        <View style={ styles.outline }>{ top }</View>
        <View style={ styles.outline }>{ topLeft }</View>
        <View style={ styles.outline }>{ topRight }</View>
        <View style={ styles.outline }>{ bottomLeft }</View>
        { bottomRight }
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

如果文字不大,也可以只用 4 个方向而不是 8 个方向来提高性能:

<View>
    <View style={ styles.outline }>{ topLeft }</View>
    <View style={ styles.outline }>{ topRight }</View>
    <View style={ styles.outline }>{ bottomLeft }</View>
    { bottomRight }
</View>
Run Code Online (Sandbox Code Playgroud)

用法:

<TextStroke stroke={ 2 } color={ '#000000' }>
  <Text style={ {
    fontSize: 100,
    color: '#FFFFFF'
  } }> Sample </Text>
</TextStroke>
Run Code Online (Sandbox Code Playgroud)

您还可以在内部使用多个 Text 对象,因为包装器会复制所有这些对象。

表现:

我还没有检查过这个解决方案的性能。由于我们多次复制文本,因此可能不是很好。

问题:

需要注意stroke价值。使用更高的值,阴影的边缘将是可见的。如果你真的需要更宽的笔触,你可以通过添加更多层来覆盖不同的阴影方向来解决这个问题。

宽行程样本