unsupported dashed / dotted border style

516 阅读1分钟

react native unsupported dashed / dotted border style

React Native 不支持在 borderStyle 中使用虚线或点状线样式,但你可以通过使用 react-native-svg 包来实现一个带虚线或点状线样式的边框。

下面是一些使用 react-native-svg 实现虚线或点状线样式边框的示例代码:

  1. 使用 react-native-svg 的 <Svg> 和 <Line> 组件实现虚线样式边框:
jsx
复制代码
import React from 'react';
import { View } from 'react-native';
import Svg, { Line } from 'react-native-svg';

const DashedBorder = ({ color }) => {
  return (
    <Svg height="100%" width="100%" viewBox="0 0 100 100" style={{ position: 'absolute' }}>
      <Line x1="0" y1="0" x2="100" y2="0" stroke={color} strokeDasharray="5,5" strokeWidth="2" />
      <Line x1="0" y1="100" x2="100" y2="100" stroke={color} strokeDasharray="5,5" strokeWidth="2" />
      <Line x1="0" y1="0" x2="0" y2="100" stroke={color} strokeDasharray="5,5" strokeWidth="2" />
      <Line x1="100" y1="0" x2="100" y2="100" stroke={color} strokeDasharray="5,5" strokeWidth="2" />
    </Svg>
  );
};

const MyComponent = () => {
  return (
    <View style={{ width: 200, height: 200 }}>
      <DashedBorder color="red" />
      {/* Your other components */}
    </View>
  );
};
  1. 使用 react-native-svg 的 <Svg> 和 <Rect> 组件实现点状线样式边框:
jsx
复制代码
import React from 'react';
import { View } from 'react-native';
import Svg, { Rect } from 'react-native-svg';

const DottedBorder = ({ color, strokeWidth }) => {
  return (
    <Svg height="100%" width="100%" viewBox="0 0 100 100" style={{ position: 'absolute' }}>
      <Rect x="0" y="0" width="100" height="100" stroke={color} strokeWidth={strokeWidth} fill="transparent" strokeDasharray={`${strokeWidth}, ${strokeWidth}`} />
    </Svg>
  );
};

const MyComponent = () => {
  return (
    <View style={{ width: 200, height: 200 }}>
      <DottedBorder color="red" strokeWidth={2} />
      {/* Your other components */}
    </View>
  );
};

希望这些示例代码能帮到你。如果你还有其他问题,请随时继续提问。