I'm working on building a React Native app based on designs from our designer. The design has several places where there are buttons or shapes with one diagonal line (see the following example). I've tried using SkewX
but that just seems to rotate the whole shape (and doesn't seem to work on Android anyway). How can I draw a rectangle/button with a diagonal border on one side?
我正在根据设计师的设计构建React Native应用程序。该设计有几个地方有按钮或具有一条对角线的形状(参见下面的例子)。我已经尝试过使用SkewX,但这似乎只是旋转了整个形状(并且似乎无论如何都不适用于Android)。如何在一侧绘制一个带有对角线边框的矩形/按钮?
2 个解决方案
#1
19
You can apply css to View
class and create the desired output, Heres a small demo code edited version
您可以将css应用于View类并创建所需的输出,这是一个小的演示代码编辑版本
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.triangleCorner}></View>
<View style={styles.triangleCornerLayer}></View>
<View style={styles.triangleCorner1}></View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},triangleCorner: {
position: 'absolute',
top:105,
left:0,
width: 300,
height: 100,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 50,
borderTopWidth: 80,
borderRightColor: 'transparent',
borderTopColor: 'gray'
},triangleCorner1: {
position: 'absolute',
top:100,
left:0,
width: 130,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 50,
borderTopWidth: 90,
borderRightColor: 'transparent',
borderTopColor: 'green'
},triangleCornerLayer: {
position: 'absolute',
top:107,
left:0,
width:297,
height: 100,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 47,
borderTopWidth: 75,
borderRightColor: 'transparent',
borderTopColor: 'white'
}
});
Result:
#2
-1
Use CALayer
for that kind of shape.
将CALayer用于这种形状。
Below code for that:
下面的代码:
let layer = CAShapeLayer()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 150, y: 0))
path.addLine(to: CGPoint(x: 100, y: 50))
path.addLine(to: CGPoint(x: 0, y: 50))
path.close()
layer.path = path.cgPath
layer.fillColor = UIColor.green.cgColor
layer.strokeColor = UIColor.clear.cgColor
view.layer.addSublayer(layer)
let layer1 = CAShapeLayer()
path.move(to: CGPoint(x: 100, y: 45))
path.addLine(to: CGPoint(x: 300, y: 45))
path.addLine(to: CGPoint(x: 350, y: 5))
path.addLine(to: CGPoint(x: 150, y: 5))
path.close()
layer1.path = path.cgPath
layer1.fillColor = UIColor.clear.cgColor
layer1.strokeColor = UIColor.black.cgColor
view.layer.addSublayer(layer1)
#1
19
You can apply css to View
class and create the desired output, Heres a small demo code edited version
您可以将css应用于View类并创建所需的输出,这是一个小的演示代码编辑版本
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.triangleCorner}></View>
<View style={styles.triangleCornerLayer}></View>
<View style={styles.triangleCorner1}></View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},triangleCorner: {
position: 'absolute',
top:105,
left:0,
width: 300,
height: 100,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 50,
borderTopWidth: 80,
borderRightColor: 'transparent',
borderTopColor: 'gray'
},triangleCorner1: {
position: 'absolute',
top:100,
left:0,
width: 130,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 50,
borderTopWidth: 90,
borderRightColor: 'transparent',
borderTopColor: 'green'
},triangleCornerLayer: {
position: 'absolute',
top:107,
left:0,
width:297,
height: 100,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderRightWidth: 47,
borderTopWidth: 75,
borderRightColor: 'transparent',
borderTopColor: 'white'
}
});
Result:
#2
-1
Use CALayer
for that kind of shape.
将CALayer用于这种形状。
Below code for that:
下面的代码:
let layer = CAShapeLayer()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 150, y: 0))
path.addLine(to: CGPoint(x: 100, y: 50))
path.addLine(to: CGPoint(x: 0, y: 50))
path.close()
layer.path = path.cgPath
layer.fillColor = UIColor.green.cgColor
layer.strokeColor = UIColor.clear.cgColor
view.layer.addSublayer(layer)
let layer1 = CAShapeLayer()
path.move(to: CGPoint(x: 100, y: 45))
path.addLine(to: CGPoint(x: 300, y: 45))
path.addLine(to: CGPoint(x: 350, y: 5))
path.addLine(to: CGPoint(x: 150, y: 5))
path.close()
layer1.path = path.cgPath
layer1.fillColor = UIColor.clear.cgColor
layer1.strokeColor = UIColor.black.cgColor
view.layer.addSublayer(layer1)