2017-06-08 11:44:04 +08:00
---
order: 2
title:
zh-CN: 自动调整字符大小
en-US: Autoset Font Size
---
## zh-CN
2020-05-25 17:56:48 +08:00
对于字符型的头像,当字符串较长时,字体大小可以根据头像宽度自动调整。也可使用 `gap` 来设置字符距离左右两侧边界单位像素。
2017-06-08 11:44:04 +08:00
## en-US
2020-05-25 17:56:48 +08:00
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. You can also use `gap` to set the unit distance between left and right sides.
2017-06-08 11:44:04 +08:00
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-05-25 17:56:48 +08:00
const GapList = [4, 3, 2, 1];
2020-01-21 16:21:37 +08:00
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-05-25 17:56:48 +08:00
const [gap, setGap] = useState(GapList[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-05-25 17:56:48 +08:00
const changeGap = () => {
const index = GapList.indexOf(gap);
setGap(index < GapList.length - 1 ? GapList [ index + 1 ] : GapList [ 0 ] ) ;
};
2020-01-21 16:21:37 +08:00
return (
2020-05-25 16:27:02 +08:00
< >
2020-05-25 17:56:48 +08:00
< Avatar style = {{ backgroundColor: color , verticalAlign: ' middle ' } } size = "large" gap = {gap} >
2020-01-21 16:21:37 +08:00
{user}
< / Avatar >
2020-03-26 15:53:57 +08:00
< Button
size="small"
style={{ margin: '0 16px', verticalAlign: 'middle' }}
onClick={changeUser}
>
2020-05-25 17:56:48 +08:00
ChangeUser
< / Button >
< Button size = "small" style = {{ verticalAlign: ' middle ' } } onClick = {changeGap} >
changeGap
2020-01-21 16:21:37 +08:00
< / Button >
2020-05-25 16:27:02 +08:00
< />
2020-01-21 16:21:37 +08:00
);
};
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
```