ant-design/components/avatar/demo/dynamic.md

49 lines
1.3 KiB
Markdown
Raw Normal View History

2017-06-08 11:44:04 +08:00
---
order: 2
title:
zh-CN: 自动调整字符大小
en-US: Autoset Font Size
---
## zh-CN
对于字符型的头像,当字符串较长时,字体大小可以根据头像宽度自动调整。
## en-US
For letter type Avatar, when the letters are too long to display, the font size can be automatically adjusted according to the width of the Avatar.
2020-01-21 16:21:37 +08:00
```tsx
2020-01-22 12:11:49 +08:00
import React, { useState } from 'react';
2017-06-08 11:44:04 +08:00
import { Avatar, Button } from 'antd';
const UserList = ['U', 'Lucy', 'Tom', 'Edward'];
2020-01-21 16:21:37 +08:00
const ColorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];
2020-01-21 17:14:58 +08:00
const Autoset: React.FC = () => {
2020-01-22 12:11:49 +08:00
const [user, setUser] = useState(UserList[0]);
const [color, setColor] = useState(ColorList[0]);
2020-01-21 16:21:37 +08:00
const changeUser = () => {
const index = UserList.indexOf(user);
setUser(index < UserList.length - 1 ? UserList[index + 1] : UserList[0]);
setColor(index < ColorList.length - 1 ? ColorList[index + 1] : ColorList[0]);
2019-05-07 14:57:32 +08:00
};
2020-01-21 16:21:37 +08:00
return (
<div>
<Avatar style={{ backgroundColor: color, verticalAlign: 'middle' }} size="large">
{user}
</Avatar>
2020-03-26 15:53:57 +08:00
<Button
size="small"
style={{ margin: '0 16px', verticalAlign: 'middle' }}
onClick={changeUser}
>
2020-01-21 16:21:37 +08:00
Change
</Button>
</div>
);
};
2017-06-08 11:44:04 +08:00
2019-03-18 15:34:02 +08:00
ReactDOM.render(<Autoset />, mountNode);
2019-05-07 14:57:32 +08:00
```