如何将字符串中的 HTML 个实体转换为 React Native 中的可读文本?
How to convert HTML entities in a string to readable text in React Native?
我从 API 中获取以下字符串,其中包含一些 html 实体。我希望将它们转换为可读文本。我该怎么做? React Native 中是否有任何内置方法可以执行相同的操作?在没有任何外部库的情况下这样做会更好。
"This era of design where there was still some "whimsy" is some of my favorite Apple work too. The austere modern era where <i>everything</i> is a slab of glass fronted metal has lost so much of the joy that was found in their earlier computers.<p>If you want a touch screen computer that used this design optimized for sketching, Microsoft took it to the logical conclusion with the Surface Studio:<p><a href=\"https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m58t4?activetab=pivot:overviewtab\" rel=\"nofollow\">https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m5...</a><p>This is much better for sketching, as can reach a much flatter angle than the G4 iMac ever could. It also holds its angle quite a bit more securely (I've used both). While I'm not a massive Windows user, the physical aspects of the Surface Studio are pretty nice and much more interesting to me than the latest iMacs."
您可以使用 react-native-render-html
呈现 html
内容。
import React from 'react';
import { useWindowDimensions } from 'react-native';
import RenderHtml from 'react-native-render-html';
const source = {
html: `
"This era of design where there was still some "whimsy" is some of my favorite Apple work too. The austere modern era where <i>everything</i> is a slab of glass fronted metal has lost so much of the joy that was found in their earlier computers.<p>If you want a touch screen computer that used this design optimized for sketching, Microsoft took it to the logical conclusion with the Surface Studio:<p><a href=\"https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m58t4?activetab=pivot:overviewtab\" rel=\"nofollow\">https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m5...</a><p>This is much better for sketching, as can reach a much flatter angle than the G4 iMac ever could. It also holds its angle quite a bit more securely (I've used both). While I'm not a massive Windows user, the physical aspects of the Surface Studio are pretty nice and much more interesting to me than the latest iMacs."`
};
export default function App() {
const { width } = useWindowDimensions();
return (
<RenderHtml source={source} contentWidth={width}/>
);
}
我从 API 中获取以下字符串,其中包含一些 html 实体。我希望将它们转换为可读文本。我该怎么做? React Native 中是否有任何内置方法可以执行相同的操作?在没有任何外部库的情况下这样做会更好。
"This era of design where there was still some "whimsy" is some of my favorite Apple work too. The austere modern era where <i>everything</i> is a slab of glass fronted metal has lost so much of the joy that was found in their earlier computers.<p>If you want a touch screen computer that used this design optimized for sketching, Microsoft took it to the logical conclusion with the Surface Studio:<p><a href=\"https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m58t4?activetab=pivot:overviewtab\" rel=\"nofollow\">https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m5...</a><p>This is much better for sketching, as can reach a much flatter angle than the G4 iMac ever could. It also holds its angle quite a bit more securely (I've used both). While I'm not a massive Windows user, the physical aspects of the Surface Studio are pretty nice and much more interesting to me than the latest iMacs."
您可以使用 react-native-render-html
呈现 html
内容。
import React from 'react';
import { useWindowDimensions } from 'react-native';
import RenderHtml from 'react-native-render-html';
const source = {
html: `
"This era of design where there was still some "whimsy" is some of my favorite Apple work too. The austere modern era where <i>everything</i> is a slab of glass fronted metal has lost so much of the joy that was found in their earlier computers.<p>If you want a touch screen computer that used this design optimized for sketching, Microsoft took it to the logical conclusion with the Surface Studio:<p><a href=\"https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m58t4?activetab=pivot:overviewtab\" rel=\"nofollow\">https://www.microsoft.com/en-us/d/surface-studio-2/8sbjxm0m5...</a><p>This is much better for sketching, as can reach a much flatter angle than the G4 iMac ever could. It also holds its angle quite a bit more securely (I've used both). While I'm not a massive Windows user, the physical aspects of the Surface Studio are pretty nice and much more interesting to me than the latest iMacs."`
};
export default function App() {
const { width } = useWindowDimensions();
return (
<RenderHtml source={source} contentWidth={width}/>
);
}