Skip to content

Icon 图标

介绍

基于图鸟图标字体的图标组件,支持字体图标和图片图标两种方式。

引入

typescript
import { TnIcon } from "@tuniao/tn-ui";

代码演示

基础用法

通过 name 属性指定图标名称。

基础用法

点我查看代码
typescript
@Entry
@ComponentV2
struct IconDemo {
  build() {
    Row({ space: 15 }) {
      TnIcon({ name: "home" })
      TnIcon({ name: "user" })
      TnIcon({ name: "set-fill" })
      TnIcon({ name: "search" })
      TnIcon({ name: "camera" })
      TnIcon({ name: "image" })
    }
  }
}

主题颜色

通过 type 属性设置主题颜色类型。

主题颜色

点我查看代码
typescript
Row({ space: 15 }) {
  TnIcon({ name: "home-capsule-fill", type: "primary" })
  TnIcon({ name: "success-circle-fill", type: "success" })
  TnIcon({ name: "warning-fill", type: "warning" })
  TnIcon({ name: "close-circle", type: "danger" })
  TnIcon({ name: "notice-fill", type: "info" })
}

自定义颜色

通过 color 属性设置自定义颜色。

自定义颜色

点我查看代码
typescript
Row({ space: 15 }) {
  TnIcon({ name: "home-capsule-fill", color: "#f56c6c" })
  TnIcon({ name: "home-capsule-fill", color: "#67c23a" })
  TnIcon({ name: "home-capsule-fill", color: "#409eff" })
}

图标尺寸

通过 iconSize 属性设置图标大小。

图标尺寸

点我查看代码
typescript
Row({ space: 15 }) {
  TnIcon({ name: "home-capsule-fill", type: "primary", iconSize: 20 });
  TnIcon({ name: "home-capsule-fill", type: "primary", iconSize: 28 });
  TnIcon({ name: "home-capsule-fill", type: "primary", iconSize: 36 });
  TnIcon({ name: "home-capsule-fill", type: "primary", iconSize: 48 });
  TnIcon({ name: "home-capsule-fill", type: "primary", iconSize: 64 });
}

加粗

通过 bold 属性使图标加粗显示。

加粗图标

点我查看代码
typescript
Row({ space: 15 }) {
  TnIcon({ name: "home-capsule-fill" })
  TnIcon({ name: "home-capsule-fill", bold: true })
}

图片图标

name 属性传入图片路径即可渲染图片图标。

图片图标

点我查看代码
typescript
Row({ space: 15 }) {
  TnIcon({ name: $r("app.media.icon_example"), iconSize: 40 })
  TnIcon({ name: "https://vue3.tuniaokj.com/images/tuniao-logo.svg", iconSize: 40 })
}

圆角

通过 bgColor 设置背景色,iconPadding 设置图标与背景边缘的内间距,配合 iconRadius 实现带背景的圆角图标。

背景色图标

点我查看代码
typescript
Row({ space: 15 }) {
  // 无圆角
  TnIcon({
    name: "home-capsule-fill",
    color: Color.White,
    iconSize: 28,
    bgColor: $r("app.color.tn_color_primary"),
    iconPadding: 10,
    iconRadius: 0
  });
  // 小圆角
  TnIcon({
    name: "home-capsule-fill",
    color: Color.White,
    iconSize: 28,
    bgColor: $r("app.color.tn_color_success"),
    iconPadding: 10,
    iconRadius: 8
  });
  // 全圆角
  TnIcon({
    name: "home-capsule-fill",
    color: Color.White,
    iconSize: 28,
    bgColor: $r("app.color.tn_color_warning"),
    iconPadding: 10,
    iconRadius: 24
  });
}

点击事件

通过 onIconClick 回调处理点击事件。

点我查看代码
typescript
TnIcon({
  name: "star",
  type: "primary",
  onIconClick: () => {
    console.info("图标被点击了");
  },
})

API

Props

参数说明类型默认值
name图标名称或图片路径string | Resource-
color自定义图标颜色ResourceColor-
type主题颜色类型TnComponentColorType-
iconSize图标尺寸number | string-
bold是否加粗booleanfalse
iconRadius图标圆角Length0
bgColor图标背景色,设置后图标会包裹在带背景色的容器中ResourceColor-
iconPadding图标内间距,配合 bgColor 使用number | Padding-
customFontName自定义字体名称string-

Events

事件名说明回调参数
onIconClick点击图标时触发(event: ClickEvent) => void

类型定义

TnComponentColorType

typescript
type TnComponentColorType = "primary" | "success" | "warning" | "danger" | "error" | "info";