目录

Select组件默认加粗字体

问题现象

在字体大小一致的前提下,Select组件中显示的文本相较Text组件中的文本,视觉上更为粗重。

问题代码示例参考如下:

@Entry
@Component
struct SelectExample {
  @State text: string = '这是实验组';
  @State index: number = 0;

  build() {
    Column({ space: 10 }) {
      // icon可以使用其他有效资源
      Select([{ value: 'aaa', icon: $r('app.media.startIcon') }])
        .selected(this.index)
        .value(this.text)
        .font({ size: 35 })
        .optionWidth(200)
        .optionHeight(300)
        .onSelect((index: number, text?: string | undefined) => {
          this.index = index;
          if (text) {
            this.text = text;
          }
        });
      Text('这是对照组')
        .fontSize(35);
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center);
  }
}

问题效果预览:

效果预览

背景知识

问题定位

分析结论

Select组件默认会加粗字体,值默认为FontWeight.Medium,若需要修改字体粗细样式,则应该显式地定义其weight参数。

修改建议

为Select组件的font属性添加weight参数,并将值设置为FontWeight枚举类型的Normal。

完整示例参考如下:

@Entry
@Component
struct SelectBold {
  @State text: string = '这是实验组';
  @State index: number = 0;

  build() {
    Column({ space: 10 }) {
      // icon可以使用其他有效资源
      Select([{ value: 'aaa', icon: $r('app.media.startIcon') }])
        .selected(this.index)
        .value(this.text)
        .font({ size: 35, weight: FontWeight.Normal })
        .optionWidth(200)
        .optionHeight(300)
        .onSelect((index: number, text?: string | undefined) => {
          this.index = index;
          if (text) {
            this.text = text;
          }
        });
      Text('这是对照组')
        .fontSize(35);
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center);
  }
}