如何在React Native应用程序中显示超链接?
例如
<a href="https://google.com>Google</a>
Run Code Online (Sandbox Code Playgroud)
Phi*_*sen 165
像这样的东西:
IOS:
<Text style={{color: 'blue'}}
onPress={() => Linking.openURL('http://google.com')}>
Google
</Text>
Run Code Online (Sandbox Code Playgroud)
使用Linking与React Native捆绑在一起的模块.
安卓:
<Text style={{color: 'blue'}}
onPress={() => Linking.openURL('http://google.com')}>
Google
</Text>
Run Code Online (Sandbox Code Playgroud)
使用Linking与React Native捆绑在一起的模块.
Kuf*_*Kuf 22
所选答案仅指iOS.对于这两个平台,您可以使用以下组件:
import React, { Component, PropTypes } from 'react';
import {
Linking,
Text,
StyleSheet
} from 'react-native';
export default class HyperLink extends Component {
constructor(){
super();
this._goToURL = this._goToURL.bind(this);
}
static propTypes = {
url: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
}
render() {
const { title} = this.props;
return(
<Text style={styles.title} onPress={this._goToURL}>
> {title}
</Text>
);
}
_goToURL() {
const { url } = this.props;
Linking.canOpenURL(url).then(supported => {
if (supported) {
Linking.openURL(this.props.url);
} else {
console.log('Don\'t know how to open URI: ' + this.props.url);
}
});
}
}
const styles = StyleSheet.create({
title: {
color: '#acacac',
fontWeight: 'bold'
}
});
Run Code Online (Sandbox Code Playgroud)
Tom*_*nda 15
为此,我强烈考虑将Text组件包装在一个组件中TouchableOpacity.当TouchableOpacity触摸a时,它会消失(变得不那么不透明).这为用户在触摸文本时提供了即时反馈,并提供了改进的用户体验.
您可以使用该onPress属性TouchableOpacity来实现链接:
<TouchableOpacity onPress={() => Linking.openURL('http://google.com')}>
<Text style={{color: 'blue'}}>
Google
</Text>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)
Linking:import { Linking } from 'react-native';
const url="https://google.com"
<Text onPress={() => Linking.openURL(url)}>
{url}
</Text>
Run Code Online (Sandbox Code Playgroud)
添加到上述响应的另一个有用的注释是添加一些 flexbox 样式。这将使文本保持在一行上,并确保文本不会与屏幕重叠。
<View style={{ display: "flex", flexDirection: "row", flex: 1, flexWrap: 'wrap', margin: 10 }}>
<Text>Add your </Text>
<TouchableOpacity>
<Text style={{ color: 'blue' }} onpress={() => Linking.openURL('https://www.google.com')} >
link
</Text>
</TouchableOpacity>
<Text>here.
</Text>
</View>
Run Code Online (Sandbox Code Playgroud)
使用 React Native Hyperlink(原生<A>标签):
安装:
npm i react-native-a
Run Code Online (Sandbox Code Playgroud)
进口:
import A from 'react-native-a'
Run Code Online (Sandbox Code Playgroud)
用法:
<A>Example.com</A><A href="example.com">Example</A><A href="https://example.com">Example</A><A href="example.com" style={{fontWeight: 'bold'}}>Example</A>以下是如何实现一个带下划线的超链接,并且具有单击时更改颜色的网络标准行为(如 CSS a:active)。
import { Linking, Pressable, Text } from 'react-native';
<Pressable onPress={() => Linking.openURL('https://example.com')}>
{({ pressed }) =>
<Text style={{
textDecorationLine: 'underline',
color: pressed ? 'red' : 'blue'
}}>I'm a hyperlink!</Text>
}
</Pressable>
Run Code Online (Sandbox Code Playgroud)
Text,但文档指出它比 更“面向未来” ,所以我们改用。LinkingTouchableOpacityPressableTouchableOpacityPressableText其本身实际上具有onPress()属性,因此TouchableOpacity对于简单地处理新闻来说是不必要的。然而,仅用它来实现颜色样式的改变似乎是不可能的Text。| 归档时间: |
|
| 查看次数: |
66565 次 |
| 最近记录: |