I am using react-native-dynamic-fonts to inject dynamic fonts at run time.
I downloaded Spicy Rice from Google Fonts and I am using it like so:
export default class App extends Component<Props> {
componentWillMount() {
loadFonts([{name: 'SpicyRice-Regular', data: font, type: 'ttf'}], true).then(function(names) {
console.log('Loaded all fonts successfully. Font names are: ', names);
});
}
render() {
return (
<View style={{flex: 1, marginTop: 150}}>
<Text style={{fontFamily: 'SpicyRice-Regular', fontSize: 25, color: 'red'}}>Foo</Text>
<WebView style={{ flex: 1, }} source={content} />
</View>
);
}
}
I see my console log and it returns SpicyRice-Regular. However, when I try to use it in my WebView on Android, it doesn't seem to work.
My WebView simply uses:
p {
font-size: 150px;
font-family: 'SpicyRice-Regular';
font-weight: 400;
font-style: normal;
}
However, it renders on iOS but doesn't on Android.
iOS:
Android:
Font's don't waterfall through for Android Webview. Just like the Chrome browser, each instance of a Webview is sandboxed into its own environment.
You do have access to the filesystem though and you can to link directly to the font asset using url("file:///android_asset/fonts/<filename>") in the Webview CSS properties so something like this should work:
p {
font-size: 150px;
font-family: 'SpicyRice-Regular';
font-weight: 400;
font-style: normal;
src: url('file:///android_asset/fonts/SpicyRice-Regular.ttf')
}
Related
How can I properly use sprite sheets in react-native? Or do people typically load icons separately and not use sprite sheets? Mind you this is not sprites for animation, just pure icons.
For example here is my sheet...
And here is the css for one icon...
.Sprite {
background-image: url(./assets/images/spritesheet.png);
background-repeat: no-repeat;
display: inline-block;
}
.IconMenu30px {
width: 30px;
height: 30px;
background-position: -53px -5px;
}
And I tried to translate this into React Native like this...
<Image source={require("../assets/images/spritesheet.png")} style={styles.menuIcon} />
const styles = StyleSheet.create({
menuIcon: {
width: 30,
height: 30,
},
})
But apparently there is no background position attribute in React Native.
Did you try
<ImageBackground />
This might help with your issue
I have a simple Button Component in my react native app, made with just styled-components.
This is what it looks like:
const ButtonContainer = styled.TouchableOpacity`
border-radius: 12px;
margin: 6px;
flex-basis: ${props => props.flexBasis || "20%"};
background: ${props => props.backgroundColor || "orange"};
justify-content: center;
align-items: center;
flex: ${props => props.flex || 0};
`;
So when I pass in a flex prop like this <ButtonContainer flex="1" /> I want it to fill the available space. It works as expected.
But when I don't pass in the flex prop, I want it to behave like I never set it. (Don't take up the whole available space).
This does not work. It's still taking up all the available space.
When I get rid of the line flex: ${props => props.flex || 0}; all together it's working, but I want to set it to flex: 1 sometimes (reusable component)
So what is the default flex setting in a react-native component?
I already tried undefined, null and -1 but no effect at all.
setting flex: none worked for me,
so -
const Component = styled.View`
flex: ${props => props.flex || 'none'};
`;
I have a small problem with triggering rotation event in react-native.
I receive the error that this.setState is undefined. When I use this.onLayout.bind(this), still nothing happend.
EDITED
App
const win = Dimensions.get('window');
constructor(props) {
super(props);
this.state = {
height: win.height,
width: win.width
}
}
onLayout (e) {
console.log(e);
this.setState({
height: e.nativeEvent.layout.height,
width: e.nativeEvent.layout.width,
})
}
render() {
return <View style={[styles.container, {width: this.state.width, height: this.state.height }]} onLayout={this.onLayout}>
<WebView
source={webapp2}
originWhitelist={'["*"]'}
automaticallyAdjustContentInsets={true}
allowFileAccess={true}
javaScriptEnabled={true}
domStorageEnabled={true}
scalesPageToFit={true}
scrollEnabled={false}
mixedContentMode='always'
/>
</View>
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
}
});
index.html (webapp)
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0" />
<style media="screen" type="text/css">
#container, #test {
width:100%;
height:100%;
top:0;
left:0;
right:0;
bottom:0;
position:absolute;
user-select: none;
-webkit-user-select: none;
border:1px solid red;
}
</style>
</head>
<body>
<br><br><br><br><br><h1>aaaa</h1>
<div id="test"></div>
</body>
</html>
Also the event is not triggered when I use simple <Text>
if you use a method like
onLayout (e) {
console.log(e);
this.setState({
height: e.nativeEvent.layout.height,
width: e.nativeEvent.layout.width,
})
You have to bind it in constructor like :
this.onLayout = this.onLayout.bind(this)
I had recently similar issue and I know what cause it but not how to resolve.
Here is a snack.expo example: Link
Try to uncomment
/* {
width: width,
height: height,
}, */
an instantly onLayout stop running.
If you are using as style a value setted by an state and this state is updated by onLayout function, this is only triggered twice times.
I means, this looks incompatible or maybe somehow is possible manage it.
I'm begginer with React Native,
Do someone know something about this behavoir and how "to fix" it ?
I want to set a video as a background but I just could set a video on the screen, not as a background, and I want to have some buttons on it.
I have seen some questions but using react-native video library, and I am using
import { Video } from 'expo';
I want to play a video as a background with expo library, hope somebody can help me, thanks in advance
You can achieve it using zIndex, following code will render video as background and play button on top of it. React native styling works same as web CSS, You should read more about css positioning here.
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button}>
<Text>Play</Text>
</TouchableOpacity>
<Video style={styles.video} />
</View>
);
}
const styles = {
container: {
position: 'relative'
},
video: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
zIndex: 1,
},
button: {
position: 'relative',
zIndex: 2,
}
};
I need to make my View circular on a non solid colored background.
I tried using borderRadius and overflow: 'hidden', but it isn't working. I see that this is a known issue with React Native: https://github.com/facebook/react-native/issues/3198
There seems to be workarounds for images that are on top of a solid background, but my background is dynamic and on top of an image so therefore I can't hardcode it.
Are there any alternatives to get something like this to work?
The black square should be a circle:
Here's the code (P.S. I'm using react native webrtc, that's where I'm getting RTCView, but I think this works with a plain old View):
const styles = StyleSheet.create({
remoteVideoContainer: {
borderRadius: 50,
height: 100,
marginBottom: 20,
width: 100,
overflow: 'hidden',
backgroundColor: 'green',
},
video: {
flex: 1,
resizeMode: 'cover',
backgroundColor: 'blue',
},
});
export default VideoView = ({
videoURL,
}) => {
return (
<View style={styles.remoteVideoContainer}>
<RTCView
style={styles.video}
streamURL={videoURL}
mirror={true}
objectFit="cover"
/>
</View>
);
};
I'm not even sure if you can style the RTCView. The code doesn't expose a styling prop. https://github.com/oney/react-native-webrtc/blob/master/RTCView.js