Skip to content

文字、素材与图标 ​

Text 显示文字,Image 显示已经导入的本地素材,Symbol 显示系统 SF Symbols 图标。它们共享尺寸、位置、透明度和基础效果属性。

Text ​

属性值用途
fontSize数字,点字号
fontWeightultraLight、thin、light、regular、medium、semibold、bold、heavy、black字重
fontDesigndefault、rounded、monospaced、serif字体设计
monospacedDigits布尔值,默认 true数字等宽,减少读数变动时的跳动
letterSpacing数字字距
color绘图颜色文字颜色
alignmentleading、center、trailing对齐
minimumScaleFactor0.01…1受限尺寸中可缩小到原字号的比例
contentAnimation对象文字内容变化时的过渡
jsx
<Text width={160} height={80} fontSize={64} fontWeight="light"
  fontDesign="rounded" monospacedDigits={true}
  minimumScaleFactor={0.5} color={$env.foreground}
  contentAnimation={{type: 'numericText'}}>
  {$data.current.speed === null ? '—' : Math.round($data.current.speed)}
</Text>

字数较长时,不要只依赖缩小字号;也应缩短标签、增加宽度或调整信息层级。组件没有网页 font-family 或 CSS 样式对象。

Image ​

属性值用途
source本地素材标记例如素材管理复制出的哈希文件名
contentModefit / fill完整显示或填充裁切
width / height数字、auto、fill素材显示区域
color颜色,可选适用图像的颜色处理;照片通常保留原色
jsx
<Image source="badge.png" width={64} height={64} contentMode="fit"/>

上例的名称只有在对应素材存在时才有效。source 不是网络地址,不要填写 https://... 或 iOS 本地绝对路径。

Symbol ​

jsx
<Symbol name="location.north.fill" width={24} height={32} color={$env.accent}/>
<Symbol name="speedometer" width={24} height={24} color={$env.foreground}/>

图标名称需在设备系统支持的 SF Symbols 范围内。不同 iOS 版本可用的名称不完全相同,建议使用常见名称,并在目标设备预览。

Image source="system:location.fill" 也可引用系统图标;一般使用 Symbol 更直观。名称 Symbol 在 JSX 中指绘图组件,不影响普通 JavaScript 的 Symbol 标准能力。

文字和图标并排 ​

jsx
function main() {
  return <HStack spacing={6}>
    <Symbol name="location.fill" width={14} height={14} color={$env.accent}/>
    <Text fontSize={14} color={$env.foreground}>
      {$data.current.heading === null ? '—' : Math.round($data.current.heading) + '°'}
    </Text>
  </HStack>;
}

为需要旋转的图标外包一层 Transform。不要直接把 rotation 传给 Symbol。