Skip to content

Instantly share code, notes, and snippets.

@zi6xuan
Created May 24, 2018 05:54
Show Gist options
  • Save zi6xuan/357796652fdb05d41f4eb0228668e5d7 to your computer and use it in GitHub Desktop.
Save zi6xuan/357796652fdb05d41f4eb0228668e5d7 to your computer and use it in GitHub Desktop.
react-native text support url link
richText(str: string) {
//
let strAry = new Array();
let startI = 0;
let urls = str.match(/<https?\:\/\/.+?>/g);
let textAry = str.split(/<https?\:\/\/.+?>/);
let viewAry = new Array();
for (let i = 0; i < textAry.length; i++) {
let urlCom = null;
if (urls != null && i < urls.length) {
let url = urls[i].replace("<", "").replace(">", "");
urlCom = (
<Text onPress={(event) => this.onLink(url)}
style={{ color: '#0000FF', borderBottomColor: '#0000FF', borderBottomWidth: 1 }}>
{url}
</Text>
);
}
viewAry.push(
<Text key={i}>
{textAry[i]}
{urlCom}
</Text>
);
}
return (
<Text style={[styles.text, styles.textPadding]}>
{viewAry}
</ Text>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment