图标小符号 (SymbolGlyph/SymbolSpan)

SymbolGlyph是图标小符号组件,便于使用精美的图标,如渲染多色图标和使用动效图标。SymbolSpan作为Text组件的子组件,可在文本中穿插显示图标小符号。具体用法请参考SymbolGlyphSymbolSpan组件的文档。

创建图标

SymbolGlyph通过$r引用Resource资源来创建,目前仅支持系统预置的Symbol资源名。

SymbolGlyph($r('sys.symbol.ohos_folder_badge_plus'))
  .fontSize(96)
  .renderingStrategy(SymbolRenderingStrategy.SINGLE)
  .fontColor([Color.Black, Color.Green, Color.White])

symbol_folder_badge_plus

添加到文本中

SymbolSpan可作为Text的子组件用于显示图标小符号。可以在一个Text组件内添加多个SymbolSpan,从而展示一串连续的图标。

  • 创建SymbolSpan。

    SymbolSpan组件需嵌入在Text组件中才能显示,单独使用不会呈现任何内容。

    Text() {
      SymbolSpan($r('sys.symbol.ohos_trash'))
        .fontWeight(FontWeight.Normal)
        .fontSize(96)
    }
    

    symbol_trash

  • 通过fontSize属性设置SymbolSpan的大小。

    Row() {
      Column() {
        Text('48')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(48)
            .renderingStrategy(SymbolRenderingStrategy.SINGLE)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    
      Column() {
        Text('72')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(72)
            .renderingStrategy(SymbolRenderingStrategy.SINGLE)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    
      Column() {
        Text('96')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .renderingStrategy(SymbolRenderingStrategy.SINGLE)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    }
    

    symbolSpan_multi_fontSize

  • 通过fontWeight属性设置SymbolSpan组件的粗细。

    Row() {
      Column() {
        Text('Light')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_trash'))
            .fontWeight(FontWeight.Lighter)
            .fontSize(96)
        }
      }
    
      Column() {
        Text('Normal')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_trash'))
            .fontWeight(FontWeight.Normal)
            .fontSize(96)
        }
      }
    
      Column() {
        Text('Bold')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_trash'))
            .fontWeight(FontWeight.Bold)
            .fontSize(96)
        }
      }
    }
    

    symbolSpan_multi_fontWeight_trash

  • 通过fontColor属性设置SymbolSpan的颜色。

    Row() {
      Column() {
        Text('Black')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .fontColor([Color.Black])
        }
      }
    
      Column() {
        Text('Green')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .fontColor([Color.Green])
        }
      }
    
      Column() {
        Text('Pink')
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .fontColor([Color.Pink])
        }
      }
    }
    

    symbolSpan_multi_fontColor

  • 通过renderingStrategy属性设置SymbolSpan的渲染策略。

    Row() {
      Column() {
        // 'app.string.single_color'资源文件中的value值为"单色"
        Text($r('app.string.single_color'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .renderingStrategy(SymbolRenderingStrategy.SINGLE)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    
      Column() {
        // 'app.string.multi_color'资源文件中的value值为"多色"
        Text($r('app.string.multi_color'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .renderingStrategy(SymbolRenderingStrategy.MULTIPLE_COLOR)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    
      Column() {
        // 'app.string.hierarchical'资源文件中的value值为"分层"
        Text($r('app.string.hierarchical'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_folder_badge_plus'))
            .fontSize(96)
            .renderingStrategy(SymbolRenderingStrategy.MULTIPLE_OPACITY)
            .fontColor([Color.Black, Color.Green, Color.White])
        }
      }
    }
    

    symbolSpan_multi_renderingStrategy

  • 通过effectStrategy属性设置SymbolSpan的动效策略。

    Row() {
      Column() {
        // 'app.string.no_action'资源文件中的value值为"无动效"
        Text($r('app.string.no_action'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_wifi'))
            .fontSize(96)
            .effectStrategy(SymbolEffectStrategy.NONE)
        }
      }
    
      Column() {
        // 'app.string.overall_scaling_animation_effect'资源文件中的value值为"整体缩放动效"
        Text($r('app.string.overall_scaling_animation_effect'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_wifi'))
            .fontSize(96)
            .effectStrategy(SymbolEffectStrategy.SCALE)
        }
      }
    
      Column() {
        // 'app.string.hierarchical_animation'资源文件中的value值为"层级动效"
        Text($r('app.string.hierarchical_animation'));
        Text() {
          SymbolSpan($r('sys.symbol.ohos_wifi'))
            .fontSize(96)
            .effectStrategy(SymbolEffectStrategy.HIERARCHICAL)
        }
      }
    }
    

    symbolSpan_multi_effectStrategy

  • SymbolSpan不支持通用事件。

自定义图标动效

相较于effectStrategy属性在启动时即触发动效,可以通过以下两种方式来控制动效的播放状态,以及选择更多样化的动效策略。

关于effectStrategy属性与symbolEffect属性的多种动态属性使用及生效原则,详情请参阅SymbolGlyph.symbolEffect属性的说明。

  • 通过设置SymbolEffect属性,可以同时配置SymbolGlyph的动效策略和播放状态。

    @State isActive: boolean = true;
    
    Column() {
      // 'app.string.variable_color_animation'资源文件中的value值为"可变颜色动效"
      Text($r('app.string.variable_color_animation'));
      SymbolGlyph($r('sys.symbol.ohos_wifi'))
        .fontSize(96)
        .symbolEffect(new HierarchicalSymbolEffect(EffectFillStyle.ITERATIVE), this.isActive)
      // 'app.string.off'资源文件中的value值为"关闭"
      // 'app.string.on'资源文件中的value值为"播放"
      Button(this.isActive ? $r('app.string.off') : $r('app.string.on')).onClick(() => {
        this.isActive = !this.isActive;
      })
    }
    

    symbolGlyph_symbolEffect_isActive

  • 通过设置SymbolEffect属性,可以同时指定SymbolGlyph的动画效果策略及其播放触发条件。

    @State triggerValueReplace: number = 0;
    
    Column() {
      // 'app.string.bounce_animation'资源文件中的value值为"弹跳动效"
      Text($r('app.string.bounce_animation'));
      SymbolGlyph($r('sys.symbol.ellipsis_message_1'))
        .fontSize(96)
        .fontColor([Color.Gray])
        .symbolEffect(new BounceSymbolEffect(EffectScope.WHOLE, EffectDirection.UP),
                      this.triggerValueReplace)
      Button('trigger').onClick(() => {
        this.triggerValueReplace = this.triggerValueReplace + 1;
      })
    }
    

    BounceSymbolEffect

  • 从API version 20开始,支持通过设置SymbolEffect属性为ReplaceSymbolEffect,设置ReplaceEffectType为ReplaceEffectType.SLASH_OVERLAY,可以指定SymbolGlyph的禁用动画效果及其播放触发条件。

    @State triggerValueReplace: number = 0;
    replaceFlag: boolean = true;
    @State renderMode: number = 1;
    
    Column() {
      // 'app.string.disable_animation'资源文件中的value值为"禁用动效"
      Text($r('app.string.disable_animation'));
      SymbolGlyph(this.replaceFlag ? $r('sys.symbol.eye_slash') : $r('sys.symbol.eye'))
        .fontSize(96)
        .renderingStrategy(this.renderMode)
        .symbolEffect(new ReplaceSymbolEffect(EffectScope.LAYER, ReplaceEffectType.SLASH_OVERLAY),
                      this.triggerValueReplace)
      Button('trigger').onClick(() => {
        this.replaceFlag = !this.replaceFlag;
        this.triggerValueReplace = this.triggerValueReplace + 1;
      })
    }
    

    symbolGlyph_symbolEffect_disable

  • 从API version 20开始,支持通过设置SymbolEffect属性为ReplaceSymbolEffect,设置ReplaceEffectType为ReplaceEffectType.CROSS_FADE,可以指定SymbolGlyph的快速替换动画效果及其播放触发条件。

    @State triggerValueReplace: number = 0;
    replaceFlag: boolean = true;
    
    Column() {
      // 'app.string.quick_replacement_animation'资源文件中的value值为"快速替换动效"
      Text($r('app.string.quick_replacement_animation'));
      SymbolGlyph(this.replaceFlag ? $r('sys.symbol.checkmark_circle') : $r('sys.symbol.repeat_1'))
        .fontSize(96)
        .symbolEffect(new ReplaceSymbolEffect(EffectScope.WHOLE, ReplaceEffectType.CROSS_FADE),
                      this.triggerValueReplace)
      Button('trigger').onClick(() => {
        this.replaceFlag = !this.replaceFlag;
        this.triggerValueReplace = this.triggerValueReplace + 1;
      })
    }
    

    symbolGlyph_symbolEffect_quick_replace

设置阴影和渐变色

  • 从API version 20开始,支持通过symbolShadow接口实现了symbolGlyph组件显示阴影效果。

    @State isActive: boolean = true;
    
    options: ShadowOptions = {
      radius: 10.0,
      color: Color.Blue,
      offsetX: 10,
      offsetY: 10,
    };
    
    Column() {
      // 'app.string.shadow_ability'资源文件中的value值为"阴影能力"
      Text($r('app.string.shadow_ability'));
      SymbolGlyph($r('sys.symbol.ohos_wifi'))
        .fontSize(96)
        .symbolEffect(new HierarchicalSymbolEffect(EffectFillStyle.ITERATIVE), !this.isActive)
        .symbolShadow(this.options)
      // 'app.string.off'资源文件中的value值为"关闭"
      // 'app.string.on'资源文件中的value值为"播放"
      Button(!this.isActive ? $r('app.string.off') : $r('app.string.on')).onClick(() => {
        this.isActive = !this.isActive;
      })
    }
    

    SymbolShadowSymbolEffect

  • 从API version 20开始,支持通过shaderStyle接口实现了symbolGlyph组件显示渐变色效果。

    radialGradientOptions: RadialGradientOptions = {
      center: ['50%', '50%'],
      radius: '20%',
      colors: [[Color.Red, 0.0], [Color.Blue, 0.3], [Color.Green, 0.5]],
      repeating: true,
    };
    
    Column() {
      // 'app.string.radial_gradient'资源文件中的value值为"径向渐变"
      Text($r('app.string.radial_gradient'))
        .fontSize(18)
        .fontColor(0xCCCCCC)
        .textAlign(TextAlign.Center)
      SymbolGlyph($r('sys.symbol.ohos_folder_badge_plus'))
        .fontSize(96)
        .shaderStyle([new RadialGradientStyle(this.radialGradientOptions)])
    }
    

    ShaderStyleSymbolEffect

添加事件

SymbolGlyph组件可以添加通用事件,例如绑定onClickonTouch等事件来响应操作。

@State wifiColor: ResourceColor = Color.Black;
SymbolGlyph($r('sys.symbol.ohos_wifi'))
  .fontSize(96)
  .fontColor([this.wifiColor])
  .onClick(() => {
    this.wifiColor = Color.Gray;
  })

symbolGlyph_onClick

场景示例

该示例通过symbolEffect、fontSize、fontColor属性展示了播放列表的效果。

// resourceGetString封装工具,从资源中获取字符串
import resourceGetString from '../../common/resource';

@Entry
@Component
struct SymbolMusicDemo {
  @State triggerValueReplace: number = 0;
  @State symbolSources: Resource[] =
    [$r('sys.symbol.repeat'), $r('sys.symbol.repeat_1'), $r('sys.symbol.arrow_left_arrow_right')];
  @State symbolSourcesIndex: number = 0;
  @State symbolText: string[] = [
    // 'app.string.play_in_order'资源文件中的value值为"顺序播放"
    resourceGetString.resourceToString($r('app.string.play_in_order')),
    // 'app.string.play_in_single_repeat'资源文件中的value值为"单曲循环"
    resourceGetString.resourceToString($r('app.string.play_in_single_repeat')),
    // 'app.string.shuffle_play'资源文件中的value值为"随机播放"
    resourceGetString.resourceToString($r('app.string.shuffle_play')),
  ];
  @State symbolTextIndex: number = 0;
  @State fontColorValue: ResourceColor = Color.Grey;
  @State fontColorValue1: ResourceColor = '#E8E8E8';

  build() {
    Column({ space: 10 }) {
      Row() {
        Text() {
          // 'app.string.current_playlist'资源文件中的value值为"当前播放列表"
          Span(resourceGetString.resourceToString($r('app.string.current_playlist')))
            .fontSize(20)
            .fontWeight(FontWeight.Bolder)
          Span('(101)')
        }
      }

      Row() {
        Row({ space: 5 }) {
          SymbolGlyph(this.symbolSources[this.symbolSourcesIndex])
            .symbolEffect(new ReplaceSymbolEffect(EffectScope.WHOLE), this.triggerValueReplace)
            .fontSize(20)
            .fontColor([this.fontColorValue])
          Text(this.symbolText[this.symbolTextIndex])
            .fontColor(this.fontColorValue)
        }
        .onClick(() => {
          this.symbolTextIndex++;
          this.symbolSourcesIndex++;
          this.triggerValueReplace++;
          if (this.symbolSourcesIndex > (this.symbolSources.length - 1)) {
            this.symbolSourcesIndex = 0;
            this.triggerValueReplace = 0;
          }
          if (this.symbolTextIndex > (this.symbolText.length - 1)) {
            this.symbolTextIndex = 0;
          }
        })
        .width('75%')

        Row({ space: 5 }) {
          Text() {
            SymbolSpan($r('sys.symbol.arrow_down_circle_badge_vip_circle_filled'))
              .fontColor([this.fontColorValue])
              .fontSize(20)
          }

          Text() {
            SymbolSpan($r('sys.symbol.heart_badge_plus'))
              .fontColor([this.fontColorValue])
              .fontSize(20)
          }

          Text() {
            SymbolSpan($r('sys.symbol.ohos_trash'))
              .fontColor([this.fontColorValue])
              .fontSize(20)
          }
        }
        .width('25%')
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.song'资源文件中的value值为"歌曲一"
          Text($r('app.string.song'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.song_again'资源文件中的value值为"歌曲二"
          Text($r('app.string.song_again'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.again_song'资源文件中的value值为"歌曲三"
          Text($r('app.string.again_song'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.song_repeat'资源文件中的value值为"歌曲四"
          Text($r('app.string.song_repeat'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.repeat_song'资源文件中的value值为"歌曲五"
          Text($r('app.string.repeat_song'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.song_play'资源文件中的value值为"歌曲六"
          Text($r('app.string.song_play'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Row() {
        Row() {
          // 'app.string.play_song'资源文件中的value值为"歌曲七"
          Text($r('app.string.play_song'))
        }.width('82%')

        Row({ space: 5 }) {
          SymbolGlyph($r('sys.symbol.play_arrow_triangle_2_circlepath'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
          SymbolGlyph($r('sys.symbol.trash'))
            .fontColor([this.fontColorValue])
            .fontSize(20)
        }
      }

      Divider().width(5).color(this.fontColorValue1).width('98%')
      Column() {
        // 'app.string.off'资源文件中的value值为"关闭"
        Text($r('app.string.off'))
      }
      .alignItems(HorizontalAlign.Center)
      .width('98%')
    }
    .alignItems(HorizontalAlign.Start)
    .width('100%')
    .height(400)
    .padding({
      left: 10,
      top: 10
    })
  }
}

symbol_scene_demo