Avatar 头像

用来显示用户头像

基本使用

```schema: scope=”body” { “type”: “avatar”, “src”: “https://suda.cdn.bcebos.com/images/amis/ai-fake-face.jpg“ }

  1. ## 文字
  2. ```schema: scope="body"
  3. {
  4. "type": "avatar",
  5. "text": "AM"
  6. }

图标

通过 icon 设置图标

```schema: scope=”body” { “type”: “avatar”, “icon”: “fa fa-user” }

  1. > 如果同时存在 srctext icon,会优先用 src、接着 text、最后 icon
  2. ## 动态图片或文字
  3. srctext 都支持变量,可以从上下文中动态获取图片或文字,下面的例子中:
  4. - 第一个获取到了,显示正常
  5. - 第二个没获取到,因此降级为显示 icon
  6. - 第三个图片没获取到,由于 text 优先级比 icon 高,所以显示 text
  7. ```schema
  8. {
  9. "data": {
  10. "myAvatar": "https://suda.cdn.bcebos.com/images/amis/ai-fake-face.jpg"
  11. },
  12. "type": "page",
  13. "body": [
  14. {
  15. "type": "avatar",
  16. "icon": "fa fa-user",
  17. "src": "$myAvatar"
  18. },
  19. {
  20. "type": "avatar",
  21. "icon": "fa fa-user",
  22. "src": "$other"
  23. },
  24. {
  25. "type": "avatar",
  26. "src": "$other",
  27. "icon": "fa fa-user",
  28. "text": "avatar"
  29. }
  30. ]
  31. }

方形和圆角形

可以通过 shape 改成方形或圆角形

```schema: scope=”body” [ { “type”: “avatar”, “shape”: “square”, “text”: “AM” }, { “type”: “avatar”, “shape”: “rounded”, “text”: “AM”, “style”: { “marginLeft”: “10px” } } ]

  1. ## 大小
  2. 通过 size 可以控制头像的大小
  3. ```schema: scope="body"
  4. [
  5. {
  6. "type": "avatar",
  7. "size": 'large',
  8. "icon": "fa fa-user"
  9. },
  10. {
  11. "type": "avatar",
  12. "size": 'default',
  13. "icon": "fa fa-user"
  14. },
  15. {
  16. "type": "avatar",
  17. "size": 'small',
  18. "icon": "fa fa-user"
  19. },
  20. {
  21. "type": "avatar",
  22. "size": 60,
  23. "src": "https://suda.cdn.bcebos.com/images/amis/ai-fake-face.jpg"
  24. },
  25. {
  26. "type": "avatar",
  27. "src": "https://suda.cdn.bcebos.com/images/amis/ai-fake-face.jpg"
  28. },
  29. {
  30. "type": "avatar",
  31. "size": 20,
  32. "src": "https://suda.cdn.bcebos.com/images/amis/ai-fake-face.jpg"
  33. },
  34. ]

控制字符类型距离左右两侧边界单位像素

通过 gap 可以控制字符类型距离左右两侧边界单位像素

```schema: scope=”body” [ { “type”: “avatar”, “text”: ‘ejson’, “gap”: 2 }, { “type”: “avatar”, “text”: “ejson”, “gap”: 7 } ]

  1. ## 图片拉伸方式
  2. 通过 `fit` 可以控制图片拉伸方式,默认是 `'cover'`
  3. ```schema: scope="body"
  4. [
  5. {
  6. "type": "avatar",
  7. "fit": "cover",
  8. "src": "https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg"
  9. },
  10. {
  11. "type": "avatar",
  12. "fit": "fill",
  13. "src": "https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg"
  14. },
  15. {
  16. "type": "avatar",
  17. "fit": "contain",
  18. "src": "https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg"
  19. },
  20. {
  21. "type": "avatar",
  22. "fit": "none",
  23. "src": "https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg"
  24. },
  25. {
  26. "type": "avatar",
  27. "fit": "scale-down",
  28. "src": "https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg"
  29. }
  30. ]

控制图片是否允许拖动

通过 draggable 可以控制图片是否允许拖动

```schema: scope=”body” [ { “type”: “avatar”, “fit”: “cover”, “src”: “https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg“, “draggable”: false }, { “type”: “avatar”, “fit”: “cover”, “src”: “https://suda.cdn.bcebos.com/images/amis/plumeria.jpeg“, “draggable”: true } ]

  1. ## 图片加载失败后,通过 onError 控制是否进行 text、icon 置换
  2. > 如果同时存在 text icon,会优先用 text、接着 icon
  3. ```schema: scope="body"
  4. {
  5. "type": "avatar",
  6. "src": "empty",
  7. "text": "avatar",
  8. "onError": "return true;"
  9. },

样式

可以通过 style 来控制背景及文字颜色

schema: scope="body" { "type": "avatar", "text": "AM", "style": { "background": "#DB3E35", "color": "#FFFFFF" } }

属性表

属性名 类型 默认值 说明
className string 外层 dom 的类名
style object 外层 dom 的样式
fit 'contain' \ 'cover' \ 'fill' \ 'none' \ 'scale-down' 'cover' 具体细节可以参考 MDN 文档
src string 图片地址
text string 文字
icon string 'fa fa-user' 图标
shape 'circle' \ 'square' \ 'rounded' 'circle' 形状,有三种 'circle' (圆形)、'square'(正方形)、'rounded'(圆角)
size number \ 'default' \ 'normal' \ 'small' 'default' `’default’ \ ‘normal’ \ ‘small’`三种字符串类型代表不同大小(分别是48、40、32),也可以直接数字表示
gap number 4 控制字符类型距离左右两侧边界单位像素
alt number 图像无法显示时的替代文本
draggable boolean 图片是否允许拖动
crossOrigin 'anonymous' \ 'use-credentials' \ '' 图片的 CORS 属性设置
onError string 图片加载失败的字符串,这个字符串是一个New Function内部执行的字符串,参数是event(使用event.nativeEvent获取原生dom事件),这个字符串需要返回boolean值。设置 "return ture;" 会在图片加载失败后,使用 text 或者 icon 代表的信息来进行替换。目前图片加载失败默认是不进行置换。注意:图片加载失败,不包括$获取数据为空情况