Typography排版

文本的基本格式。

何时使用

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

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

代码演示

Typography排版 - 图1

基本

展示文档样例。

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

Typography排版 - 图2

标题组件

展示不同级别的标题。

  1. import { Typography } from 'antd';
  2. const { Title } = Typography;
  3. ReactDOM.render(
  4. <div>
  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. </div>,
  10. mountNode,
  11. );

Typography排版 - 图3

文本与超链接组件

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

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

Typography排版 - 图4

可交互

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

  1. import { Typography } from 'antd';
  2. const { Paragraph } = Typography;
  3. class Demo extends React.Component {
  4. state = {
  5. str: 'This is an editable text.',
  6. };
  7. onChange = str => {
  8. console.log('Content change:', str);
  9. this.setState({ str });
  10. };
  11. render() {
  12. return (
  13. <div>
  14. <Paragraph editable={{ onChange: this.onChange }}>{this.state.str}</Paragraph>
  15. <Paragraph copyable>This is a copyable text.</Paragraph>
  16. <Paragraph copyable={{ text: 'Hello, Ant Design!' }}>Replace copy text.</Paragraph>
  17. </div>
  18. );
  19. }
  20. }
  21. ReactDOM.render(<Demo />, mountNode);

Typography排版 - 图5

省略号

多行文本省略。

  1. import { Typography } from 'antd';
  2. const { Paragraph } = Typography;
  3. ReactDOM.render(
  4. <div>
  5. <Paragraph ellipsis>
  6. Ant Design, a design language for background applications, is refined by Ant UED Team. Ant
  7. Design, a design language for background applications, is refined by Ant UED Team. Ant Design,
  8. a design language for background applications, is refined by Ant UED Team. Ant Design, a
  9. design language for background applications, is refined by Ant UED Team. Ant Design, a design
  10. language for background applications, is refined by Ant UED Team. Ant Design, a design
  11. language for background applications, is refined by Ant UED Team.
  12. </Paragraph>
  13. <Paragraph ellipsis={{ rows: 2, expandable: true, symbol: 'more' }}>
  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 Design,
  16. a design language for background applications, is refined by Ant UED Team. Ant Design, a
  17. design language for background applications, is refined by Ant UED Team. Ant Design, a design
  18. language for background applications, is refined by Ant UED Team. Ant Design, a design
  19. language for background applications, is refined by Ant UED Team.
  20. </Paragraph>
  21. </div>,
  22. mountNode,
  23. );

Typography排版 - 图6

后缀

添加后缀的省略。

  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. <div>
  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. </div>
  31. );
  32. }
  33. }
  34. ReactDOM.render(<Demo />, mountNode);

API

Typography.Text

参数说明类型默认值版本
code添加代码样式booleanfalse
copyable是否可拷贝,为对象时可设置复制文本以回调函数boolean | { text: string, onCopy: Function }false
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | { editing: boolean, onStart: Function, onChange: Function(string) }false
ellipsis设置自动溢出省略,需要设置元素宽度booleanfalse
mark添加标记样式booleanfalse
keyboard添加键盘样式booleanfalse4.3.0
underline添加下划线样式booleanfalse
strong是否加粗booleanfalse
type文本类型secondary | warning | danger-

Typography.Title

参数说明类型默认值版本
copyable是否可拷贝,为对象时可设置复制文本以回调函数boolean | { text: string, onCopy: Function }false
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | { editing: boolean, onStart: Function, onChange: Function(string) }false
ellipsis自动溢出省略,为对象时可设置省略行数与是否可展开等boolean | { rows: number, expandable: boolean, onExpand: Function(event), onEllipsis: Function(ellipsis) }falseonEllipsis: 4.2.0
level重要程度,相当于 h1h2h3h4number: 1, 2, 3, 41
mark添加标记样式booleanfalse
underline添加下划线样式booleanfalse
onChange当用户提交编辑内容时触发Function(string)-
type文本类型secondary | warning | danger-

Typography.Paragraph

参数说明类型默认值版本
copyable是否可拷贝,为对象时可设置复制文本以回调函数boolean | { text: string, onCopy: Function }false
delete添加删除线样式booleanfalse
disabled禁用文本booleanfalse
editable是否可编辑,为对象时可对编辑进行控制boolean | { editing: boolean, onStart: Function, onChange: Function(string) }false
ellipsis自动溢出省略,为对象时可设置省略行数、是否可展开、添加后缀等boolean | { rows: number, expandable: boolean, suffix: string, symbol: React.ReactNode, onExpand: Function(event), onEllipsis: Function(ellipsis) }falseonEllipsis: 4.2.0
mark添加标记样式booleanfalse
underline添加下划线样式booleanfalse
onChange当用户提交编辑内容时触发Function(string)-
strong是否加粗booleanfalse
type文本类型secondary | warning | danger-

FAQ

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

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

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