“Using fonts with Next.UI and react.js” 可以翻译为 “在Next.UI和react.js中使用字体”。

huangapple go评论100阅读模式
英文:

Using fonts with Next.UI and react.js

问题

我想使用一些字体,但我在查找有关如何导入等信息/文档方面遇到了困难。

我正在使用一个样式化组件库:Next.UI

在我的 index.js 中,我正在创建一个主题,并在我的 React 应用程序中使用它。该主题已经可以使用,但还有一个字体属性。

有人能给我一个使用不同字体的示例吗?我如何知道支持哪些字体?或者我如何导入字体?

提前感谢!

以下是我的代码:

  1. import { createTheme, NextUIProvider, styled } from '@nextui-org/react';
  2. const theme = createTheme({
  3. type: "light",
  4. theme: {
  5. colors: {...},
  6. fonts: {
  7. sans: 'Comic Sans MS',
  8. mono: 'Andale Mono',
  9. serif: 'Gilda Display',
  10. },
  11. }
  12. });
  13. const root = ReactDOM.createRoot(document.getElementById('root'));
  14. root.render(
  15. <NextUIProvider theme={theme}>
  16. <App />
  17. </NextUIProvider>
  18. );
  1. 希望这可以帮助您。
  2. <details>
  3. <summary>英文:</summary>
  4. I want to use some fonts, but I&#39;m having difficulties to find information/documentation about how to import etc.
  5. I am using a styled component library: [Next.UI][1].
  6. In my index.js I am creating a theme and I use it in my React application. That theme already works, but there is also a property for fonts.
  7. Can someone give me an example of using different fonts? And how do I know what fonts are supported? Or how can I import fonts?
  8. Thanks in advance!
  9. Here&#39;s my code for now:
  10. import { createTheme, NextUIProvider, styled } from &#39;@nextui-org/react&#39;;
  11. const theme = createTheme({
  12. type: &quot;light&quot;,
  13. theme: {
  14. colors: {...},
  15. fonts: {
  16. sans: &#39;Comic Sans MS&#39;,
  17. mono: &#39;Andale Mono&#39;,
  18. serif: &#39;Gilda Display &#39;,
  19. },
  20. }
  21. });
  22. const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
  23. root.render(
  24. &lt;NextUIProvider theme={theme}&gt;
  25. &lt;App /&gt;
  26. &lt;/NextUIProvider&gt;
  27. );
  28. [1]: https://nextui.org/docs/theme/customize-theme
  29. </details>
  30. # 答案1
  31. **得分**: 1
  32. 以下是翻译好的部分:
  33. styled-components模块中使用`createGlobalStyle`模块的示例...
  34. fontStyles.js
  35. ```javascript
  36. import { createGlobalStyle } from "styled-components";
  37. import RobotoWoff from "./fonts/roboto-condensed-v19-latin-regular.woff";
  38. import RobotoWoff2 from "./fonts/roboto-condensed-v19-latin-regular.woff2";
  39. const FontStyles = createGlobalStyle`
  40. @font-face {
  41. font-family: 'Roboto Condensed';
  42. src: url(${RobotoWoff2}) format('woff2'),
  43. url(${RobotoWoff}) format('woff');
  44. }
  45. `;
  46. export default FontStyles;

index.js

  1. import FontStyles from "./fontStyles";
  2. ReactDOM.render(
  3. <React.StrictMode>
  4. <FontStyles />
  5. <App />
  6. </React.StrictMode>,
  7. document.getElementById("root")
  8. );

希望这有帮助!

英文:

use createGlobalStyle module from styled-components like so...

fontStyles.js

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

  1. import { createGlobalStyle } from &quot;styled-components&quot;;
  2. import RobotoWoff from &quot;./fonts/roboto-condensed-v19-latin-regular.woff&quot;;
  3. import RobotoWoff2 from &quot;./fonts/roboto-condensed-v19-latin-regular.woff2&quot;;
  4. const FontStyles = createGlobalStyle`
  5. @font-face {
  6. font-family: &#39;Roboto Condensed&#39;;
  7. src: url(${RobotoWoff2}) format(&#39;woff2&#39;),
  8. url(${RobotoWoff}) format(&#39;woff&#39;);
  9. }
  10. `;
  11. export default FontStyles;

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

index.js

  1. import FontStyles from &quot;./fontStyles&quot;;
  2. ReactDOM.render(
  3. &lt;React.StrictMode&gt;
  4. &lt;FontStyles /&gt;
  5. &lt;App /&gt;
  6. &lt;/React.StrictMode&gt;,
  7. document.getElementById(&quot;root&quot;)
  8. );

huangapple
  • 本文由 发表于 2023年5月17日 18:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定