[英]How to put text over Image
So I'm new to react-native (and javascript in general), I'm trying to achieve this: 所以我是新的反应原生(和一般的javascript),我正在努力实现这个目标:
So basically this is an image with text over it but slightly going out of the image top. 所以基本上这是一个带有文本的图像,但略微偏离图像顶部。
And this is what I get : What I get 这就是我得到的: 我得到的
As you can see the text is not rendered when it is out of the image. 正如您所看到的那样,当文本不在图像中时,它不会被渲染。
This is the code I use : 这是我使用的代码:
class Test extends Component {
render () {
return (
<View style={styles.matchContainer}>
<Image source={ require('../../res/img/lol_wallpaper.jpg') } style={styles.backgroundImage}>
<View style={styles.topContainer}>
<Text style={styles.topText}>text</Text>
<Text style={styles.topText}>text</Text>
</View>
</Image>
</View>
);
}
}
And this is the css : 这是css:
var styles = StyleSheet.create({
matchContainer: {
flexDirection: 'column',
justifyContent: 'flex-start',
width: width,
height: 300,
marginTop: 30,
backgroundColor: '#FFFFFF',
},
topText: {
color: 'white',
fontWeight: 'bold',
backgroundColor: 'rgb(86, 203, 83)',
padding: 2,
position: 'relative',
},
topContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginLeft: 5,
marginRight: 5,
marginBottom: 5,
top: -10,
position: 'relative',
},
backgroundImage: {
width: width - 10,
height: 138,
marginLeft: 5,
marginTop: 10,
position: 'absolute',
},
});
How can I achieve this ? 我怎样才能做到这一点?
Thanks :) 谢谢 :)
I guess why you can't get your ideal answer because the Text is in the Image component. 我猜你为什么不能得到理想的答案,因为Text在Image组件中。 So when some part is out of the Image wouldn't display.
因此,当某些部分超出图像时将无法显示。
My method is use the property position . 我的方法是使用属性位置 。 There is the simple example I just created.
我刚刚创建了一个简单的例子。 https://rnplay.org/apps/rpv82A
https://rnplay.org/apps/rpv82A
And remember the property z-index in react-native is depending on the render order. 并记住react-native中的属性z-index取决于呈现顺序。 https://github.com/facebook/react-native/issues/698
https://github.com/facebook/react-native/issues/698
position
By using this method, you'll just use 3 elements: div
, img
and h2
通过使用此方法,您将只使用3个元素:
div
, img
和h2
HTML
HTML
<div class="img-box">
<img src="http://science-all.com/images/wallpapers/league-of-legends-wallpaper/league-of-legends-wallpaper-20.jpg" />
<h2>TEXT</h2>
</div>
CSS
CSS
.img-box {
margin-top: 50px;
width: 450px;
height: 200px;
position: relative;
}
.img-box img {
width: 100%;
height: auto;
}
.img-box h2 {
width: 125px;
height: 40px;
position: absolute;
top: -25%;
left: 5%;
font-size: 28pt;
text-align: center;
background-color: limegreen
}
.img-box { margin-top: 50px; width: 450px; height: 200px; position: relative; } .img-box img { width: 100%; height: auto; } .img-box h2 { width: 125px; height: 40px; position: absolute; top: -25%; left: 5%; font-size: 28pt; text-align: center; background-color: limegreen }
<div class="img-box"> <img src="http://science-all.com/images/wallpapers/league-of-legends-wallpaper/league-of-legends-wallpaper-20.jpg" /> <h2>TEXT</h2> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.