Typography排版

文本的基本格式。

何时使用

  • 当需要展示标题、段落、列表内容时使用,如文章/博客/日志的文本样式。

  • 当需要一列基于文本的基础操作时,如拷贝/省略/可编辑。

代码演示

Typography排版 - 图1

基本

展示文档样例。

  1. import { Typography, Divider } from 'antd';
  2. const { Title, Paragraph, Text, Link } = Typography;
  3. const blockContent = `AntV 是蚂蚁金服全新一代数据可视化解决方案,致力于提供一套简单方便、专业可靠、不限可能的数据可视化最佳实践。得益于丰富的业务场景和用户需求挑战,AntV 经历多年积累与不断打磨,已支撑整个阿里集团内外 20000+ 业务系统,通过了日均千万级 UV 产品的严苛考验。
  4. 我们正在基础图表,图分析,图编辑,地理空间可视化,智能可视化等各个可视化的领域耕耘,欢迎同路人一起前行。`;
  5. ReactDOM.render(
  6. <Typography>
  7. <Title>Introduction</Title>
  8. <Paragraph>
  9. In the process of internal desktop applications development, many different design specs and
  10. implementations would be involved, which might cause designers and developers difficulties and
  11. duplication and reduce the efficiency of development.
  12. </Paragraph>
  13. <Paragraph>
  14. After massive project practice and summaries, Ant Design, a design language for background
  15. applications, is refined by Ant UED Team, which aims to
  16. <Text strong>
  17. uniform the user interface specs for internal background projects, lower the unnecessary
  18. cost of design differences and implementation and liberate the resources of design and
  19. front-end development
  20. </Text>.
  21. </Paragraph>
  22. <Title level={2}>Guidelines and Resources</Title>
  23. <Paragraph>
  24. We supply a series of design principles, practical patterns and high quality design resources
  25. (<Text code>Sketch</Text> and <Text code>Axure</Text>), to help people create their product
  26. prototypes beautifully and efficiently.
  27. </Paragraph>
  28. <Paragraph>
  29. <ul>
  30. <li>
  31. <Link href="/docs/spec/proximity">Principles</Link>
  32. </li>
  33. <li>
  34. <Link href="/docs/spec/overview">Patterns</Link>
  35. </li>
  36. <li>
  37. <Link href="/docs/resources">Resource Download</Link>
  38. </li>
  39. </ul>
  40. </Paragraph>
  41. <Paragraph>
  42. Press <Text keyboard>Esc</Text> to exit...
  43. </Paragraph>
  44. <Divider />
  45. <Title>介绍</Title>
  46. <Paragraph>
  47. 蚂蚁的企业级产品是一个庞大且复杂的体系。这类产品不仅量级巨大且功能复杂,而且变动和并发频繁,常常需要设计与开发能够快速的做出响应。同时这类产品中有存在很多类似的页面以及组件,可以通过抽象得到一些稳定且高复用性的内容。
  48. </Paragraph>
  49. <Paragraph>
  50. 随着商业化的趋势,越来越多的企业级产品对更好的用户体验有了进一步的要求。带着这样的一个终极目标,我们(蚂蚁金服体验技术部)经过大量的项目实践和总结,逐步打磨出一个服务于企业级产品的设计体系
  51. Ant Design。基于<Text mark>『确定』和『自然』</Text>
  52. 的设计价值观,通过模块化的解决方案,降低冗余的生产成本,让设计者专注于
  53. <Text strong>更好的用户体验</Text>。
  54. </Paragraph>
  55. <Title level={2}>设计资源</Title>
  56. <Paragraph>
  57. 我们提供完善的设计原则、最佳实践和设计资源文件(<Text code>Sketch</Text>
  58. <Text code>Axure</Text>),来帮助业务快速设计出高质量的产品原型。
  59. </Paragraph>
  60. <Paragraph>
  61. <ul>
  62. <li>
  63. <Link href="/docs/spec/proximity-cn">设计原则</Link>
  64. </li>
  65. <li>
  66. <Link href="/docs/spec/overview-cn">设计模式</Link>
  67. </li>
  68. <li>
  69. <Link href="/docs/resources-cn">设计资源</Link>
  70. </li>
  71. </ul>
  72. </Paragraph>
  73. <Paragraph>
  74. <blockquote>{blockContent}</blockquote>
  75. <pre>{blockContent}</pre>
  76. </Paragraph>
  77. <Paragraph>
  78. 按<Text keyboard>Esc</Text>键退出阅读……
  79. </Paragraph>
  80. </Typography>,
  81. mountNode,
  82. );

Typography排版 - 图2

标题组件

展示不同级别的标题。

  1. import { Typography } from 'antd';
  2. const { Title } = Typography;
  3. ReactDOM.render(
  4. <>
  5. <Title>h1. Ant Design</Title>
  6. <Title level={2}>h2. Ant Design</Title>
  7. <Title level={3}>h3. Ant Design</Title>
  8. <Title level={4}>h4. Ant Design</Title>
  9. <Title level={5}>h5. Ant Design</Title>
  10. </>,
  11. mountNode,
  12. );

Typography排版 - 图3

文本与超链接组件

内置不同样式的文本以及超链接组件。

  1. import { Typography, Space } from 'antd';
  2. const { Text, Link } = Typography;
  3. ReactDOM.render(
  4. <Space direction="vertical">
  5. <Text>Ant Design (default)</Text>
  6. <Text type="secondary">Ant Design (secondary)</Text>
  7. <Text type="success">Ant Design (success)</Text>
  8. <Text type="warning">Ant Design (warning)</Text>
  9. <Text type="danger">Ant Design (danger)</Text>
  10. <Text disabled>Ant Design (disabled)</Text>
  11. <Text mark>Ant Design (mark)</Text>
  12. <Text code>Ant Design (code)</Text>
  13. <Text keyboard>Ant Design (keyboard)</Text>
  14. <Text underline>Ant Design (underline)</Text>
  15. <Text delete>Ant Design (delete)</Text>
  16. <Text strong>Ant Design (strong)</Text>
  17. <Text italic>Ant Design (italic)</Text>
  18. <Link href="https://ant.design" target="_blank">
  19. Ant Design (Link)
  20. </Link>
  21. </Space>,
  22. mountNode,
  23. );

Typography排版 - 图4

可交互

提供可编辑和可复制等额外的交互能力。

  1. import React, { useState } from 'react';
  2. import { Typography } from 'antd';
  3. import { HighlightOutlined, SmileOutlined, SmileFilled } from '@ant-design/icons';
  4. const { Paragraph } = Typography;
  5. const Demo: React.FC = () => {
  6. const [editableStr, setEditableStr] = useState('This is an editable text.');
  7. const [customIconStr, setCustomIconStr] = useState('Custom Edit icon and replace tooltip text.');
  8. const [hideTooltipStr, setHideTooltipStr] = useState('Hide Edit tooltip.');
  9. const [lengthLimitedStr, setLengthLimitedStr] = useState(
  10. 'This is an editable text with limited length.',
  11. );
  12. return (
  13. <>
  14. <Paragraph editable={{ onChange: setEditableStr }}>{editableStr}</Paragraph>
  15. <Paragraph
  16. editable={{
  17. icon: <HighlightOutlined />,
  18. tooltip: 'click to edit text',
  19. onChange: setCustomIconStr,
  20. }}
  21. >
  22. {customIconStr}
  23. </Paragraph>
  24. <Paragraph editable={{ tooltip: false, onChange: setHideTooltipStr }}>
  25. {hideTooltipStr}
  26. </Paragraph>
  27. <Paragraph
  28. editable={{
  29. onChange: setLengthLimitedStr,
  30. maxLength: 50,
  31. autoSize: { maxRows: 5, minRows: 3 },
  32. }}
  33. >
  34. {lengthLimitedStr}
  35. </Paragraph>
  36. <Paragraph copyable>This is a copyable text.</Paragraph>
  37. <Paragraph copyable={{ text: 'Hello, Ant Design!' }}>Replace copy text.</Paragraph>
  38. <Paragraph
  39. copyable={{
  40. icon: [<SmileOutlined key="copy-icon" />, <SmileFilled key="copied-icon" />],
  41. tooltips: ['click here', 'you clicked!!'],
  42. }}
  43. >
  44. Custom Copy icon and replace tooltips text.
  45. </Paragraph>
  46. <Paragraph copyable={{ tooltips: false }}>Hide Copy tooltips.</Paragraph>
  47. </>
  48. );
  49. };
  50. ReactDOM.render(<Demo />, mountNode);

Typography排版 - 图5

省略号

多行文本省略。你可以通过 tooltip 属性配置省略展示内容,大量文本时推荐优先使用 expandable

TypeScript

JavaScript

Typography排版 - 图6

  1. import { Typography, Switch } from 'antd';
  2. const { Paragraph, Text } = Typography;
  3. const Demo = () => {
  4. const [ellipsis, setEllipsis] = React.useState(true);
  5. return (
  6. <>
  7. <Switch
  8. checked={ellipsis}
  9. onChange={() => {
  10. setEllipsis(!ellipsis);
  11. }}
  12. />
  13. <Paragraph ellipsis={ellipsis}>
  14. Ant Design, a design language for background applications, is refined by Ant UED Team. Ant
  15. Design, a design language for background applications, is refined by Ant UED Team. Ant
  16. Design, a design language for background applications, is refined by Ant UED Team. Ant
  17. Design, a design language for background applications, is refined by Ant UED Team. Ant
  18. Design, a design language for background applications, is refined by Ant UED Team. Ant
  19. Design, a design language for background applications, is refined by Ant UED Team.
  20. </Paragraph>
  21. <Paragraph ellipsis={ellipsis ? { rows: 2, expandable: true, symbol: 'more' } : false}>
  22. Ant Design, a design language for background applications, is refined by Ant UED Team. Ant
  23. Design, a design language for background applications, is refined by Ant UED Team. Ant
  24. Design, a design language for background applications, is refined by Ant UED Team. Ant
  25. Design, a design language for background applications, is refined by Ant UED Team. Ant
  26. Design, a design language for background applications, is refined by Ant UED Team. Ant
  27. Design, a design language for background applications, is refined by Ant UED Team.
  28. </Paragraph>
  29. <Text
  30. style={ellipsis ? { width: 100 } : undefined}
  31. ellipsis={ellipsis ? { tooltip: 'I am ellipsis now!' } : false}
  32. >
  33. Ant Design, a design language for background applications, is refined by Ant UED Team.
  34. </Text>
  35. </>
  36. );
  37. };
  38. ReactDOM.render(<Demo />, mountNode);

Typography排版 - 图7

后缀

添加后缀的省略。

  1. import { Typography, Slider } from 'antd';
  2. const { Paragraph } = Typography;
  3. class Demo extends React.Component {
  4. state = {
  5. rows: 1,
  6. };
  7. onChange = rows => {
  8. this.setState({ rows });
  9. };
  10. render() {
  11. const { rows } = this.state;
  12. const article =
  13. "To be, or not to be, that is a question: Whether it is nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep To sleep- perchance to dream: ay, there's the rub! For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause. There 's the respect That makes calamity of so long life";
  14. return (
  15. <>
  16. <Slider value={rows} min={1} max={10} onChange={this.onChange} />
  17. <Paragraph
  18. ellipsis={{
  19. rows,
  20. expandable: true,
  21. suffix: '--William Shakespeare',
  22. onEllipsis: ellipsis => {
  23. console.log('Ellipsis changed:', ellipsis);
  24. },
  25. }}
  26. title={`${article}--William Shakespeare`}
  27. >
  28. {article}
  29. </Paragraph>
  30. </>
  31. );
  32. }
  33. }
  34. ReactDOM.render(<Demo />, mountNode);

API

Typography.Text

参数说明类型默认值版本
code添加代码样式booleanfalse
copyable是否可拷贝,为对象时可进行各种自定义boolean | copyablefalsecopyable
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | editablefalseeditable
ellipsis自动溢出省略booleanfalse
keyboard添加键盘样式booleanfalse4.3.0
mark添加标记样式booleanfalse
onClick点击 Text 时的回调(event) => void-
strong是否加粗booleanfalse
italic是否斜体booleanfalse4.16.0
type文本类型secondary | success | warning | danger-success: 4.6.0
underline添加下划线样式booleanfalse

Typography.Title

参数说明类型默认值版本
code添加代码样式booleanfalse
copyable是否可拷贝,为对象时可进行各种自定义boolean | copyablefalsecopyable
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | editablefalseeditable
ellipsis自动溢出省略,为对象时可设置省略行数、是否可展开、添加后缀等boolean | ellipsisfalseellipsis
level重要程度,相当于 h1h2h3h4h5number: 1, 2, 3, 4, 515: 4.6.0
mark添加标记样式booleanfalse
onClick点击 Title 时的回调(event) => void-
italic是否斜体booleanfalse4.16.0
type文本类型secondary | success | warning | danger-success: 4.6.0
underline添加下划线样式booleanfalse

Typography.Paragraph

参数说明类型默认值版本
code添加代码样式booleanfalse
copyable是否可拷贝,为对象时可进行各种自定义boolean | copyablefalsecopyable
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | editablefalseeditable
ellipsis自动溢出省略,为对象时可设置省略行数、是否可展开、添加后缀等boolean | ellipsisfalseellipsis
mark添加标记样式booleanfalse
onClick点击 Paragraph 时的回调(event) => void-
strong是否加粗booleanfalse
italic是否斜体booleanfalse4.16.0
type文本类型secondary | success | warning | danger-success: 4.6.0
underline添加下划线样式booleanfalse

copyable

  1. {
  2. text: string,
  3. onCopy: function,
  4. icon: ReactNode,
  5. tooltips: false | [ReactNode, ReactNode],
  6. }
参数说明类型默认值版本
icon自定义拷贝图标:[默认图标, 拷贝后的图标][ReactNode, ReactNode]-4.6.0
text拷贝到剪切板里的文本string-
tooltips自定义提示文案,为 false 时隐藏文案[ReactNode, ReactNode][复制, 复制成功]4.4.0
onCopy拷贝成功的回调函数function-

editable

  1. {
  2. icon: ReactNode,
  3. tooltip: boolean | ReactNode,
  4. editing: boolean,
  5. maxLength: number,
  6. autoSize: boolean | { minRows: number, maxRows: number },
  7. onStart: function,
  8. onChange: function(string),
  9. onCancel: function,
  10. onEnd: function,
  11. }
参数说明类型默认值版本
autoSize自动 resize 文本域boolean | { minRows: number, maxRows: number }-4.4.0
editing控制是否是编辑中状态booleanfalse
icon自定义编辑图标ReactNode<EditOutlined />4.6.0
maxLength编辑中文本域最大长度number-4.4.0
tooltip自定义提示文本,为 false 时关闭boolean | ReactNode编辑4.6.0
onCancel按 ESC 退出编辑状态时触发function-
onChange文本域编辑时触发function(event)-
onEnd按 ENTER 结束编辑状态时触发function-4.14.0
onStart进入编辑中状态时触发function-
onCancel按 ESC 退出编辑状态时触发function-
onEnd按 ENTER 结束编辑状态时触发function-

ellipsis

  1. {
  2. rows: number,
  3. expandable: boolean,
  4. suffix: string,
  5. symbol: ReactNode,
  6. tooltip: boolean | ReactNode,
  7. onExpand: function(event),
  8. onEllipsis: function(ellipsis),
  9. }
参数说明类型默认值版本
expandable是否可展开boolean-
rows最多显示的行数number-
suffix自定义省略内容后缀string-
symbol自定义展开描述文案ReactNode展开
tooltip省略时,展示提示信息boolean | ReactNode-4.11.0
onEllipsis触发省略时的回调function(ellipsis)-4.2.0
onExpand点击展开时的回调function(event)-

FAQ

Typography.Link 如何与 react-router 库集成?

react-router 支持自定义渲染组件:

  1. <Link to="/" component={Typography.Link} />