Skip to content

AspectRatio 宽高比 ​

保持固定宽高比的容器(对标 shadcn/ui AspectRatio 与 CSS aspect-ratio):宽度取父容器给的宽度,高度按比例算出,内容铺满整个盒子。

何时使用 ​

  • 封面图、视频占位、缩略图卡片等需要在不同宽度下保持同一比例(16:9、4:3、1:1……)的区域。
  • 需要按比例裁剪圆角:Radius 同时给盒子和内容裁圆角。
  • 只需要固定宽高的盒子用 Flex 弹性布局 的尺寸选项或 layout.W / layout.H;需要一组等宽卡片的网格用 Grid 栅格。

在线体验 ​

AspectRatio 宽高比 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

ui.AspectRatio(c, ratio, fn) 的 ratio 是宽 / 高(16.0/9 表示 16:9),fn 在盒子里构建内容,内容会被拉伸铺满整个盒子。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

func page(c *ui.Context) {
    ui.Column(c, func() {
        ui.AspectRatio(c, 16.0/9, func() {
            ui.Div(c).Bg(c.Theme.Primary.Bg)
            ui.Column(c, func() {
                ui.Text(c, "16 / 9").Strong()
            }, layout.Center())
        }).Radius(c.Theme.RadiusLG).A11yLabel("封面")
    }, layout.W(320))
}

在列里,它占满列的宽度,因此外层列(或父容器)要有确定的宽度;高度 = 宽度 / ratio。ratio 小于等于 0 或不是数字时按 1 处理。

常见用法 ​

多种比例并排 ​

放在 Row 里时,盒子本身没有宽度来源,需要用一个能分配宽度的容器包一层(例如 layout.Grow(1) 的列),或直接给 AspectRatio 传布局选项。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

func ratioRow(c *ui.Context) {
    ratios := []struct {
        name  string
        ratio float32
    }{{"16 / 9", 16.0 / 9}, {"4 / 3", 4.0 / 3}, {"1 / 1", 1}}

    ui.Row(c, func() {
        for _, r := range ratios {
            ui.Column(c, func() {
                ui.AspectRatio(c, r.ratio, func() {
                    ui.Div(c).Bg(c.Theme.Primary.Bg)
                    ui.Column(c, func() { ui.Text(c, r.name) }, layout.Center())
                }).Radius(8).A11yLabel(r.name)
            }, layout.Grow(1))
        }
    }, layout.Gap(16), layout.AlignItems(layout.AlignStart))
}

AlignStart 让各列顶对齐,高度不同的盒子不会被拉伸。

固定宽度与最大宽度 ​

没有任何东西给它宽度时(例如直接放在 Row 或叠放容器里),盒子宽度为 0,可以用布局选项给定宽度。MaxWidth(px) 限制最大宽度,高度同步缩小。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

func ratioSized(c *ui.Context) {
    ui.Row(c, func() {
        ui.AspectRatio(c, 2, func() {
            ui.Div(c).Bg(c.Theme.Primary.Bg)
        }, layout.W(200))
    })
    ui.Column(c, func() {
        ui.AspectRatio(c, 21.0/9, func() {
            ui.Div(c).Bg(c.Theme.Primary.BgHover)
        }).MaxWidth(420).Radius(c.Theme.RadiusLG)
    })
}

圆角裁剪 ​

Radius(r) 给盒子设置圆角并裁剪内容,内容不会溢出圆角;需要保持直角时不调用即可。

读取尺寸 ​

Ratio() 返回实际生效的比例(非法值已修正为 1),Rect() 返回上一帧的盒子矩形,ID() 是盒子的标识,可用于定位或测试。

go
import (
    "fmt"

    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

func ratioInfo(c *ui.Context) {
    ui.Column(c, func() {
        box := ui.AspectRatio(c, 4.0/3, func() {
            ui.Div(c).Bg(c.Theme.Primary.Bg)
        })
        r := box.Rect()
        ui.Text(c, fmt.Sprintf("比例 %.2f,上一帧尺寸 %.0f×%.0f", box.Ratio(), r.W, r.H)).Tertiary()
    }, layout.W(280))
}

主题与样式 ​

AspectRatio 本身不绘制任何东西,也没有组件令牌和 ConfigProvider.Defaults 键;背景、圆角取值请从内容和 c.Theme(例如 RadiusLG)取。三套风格下表现一致。

注意事项 ​

  • 宽度来源:高度完全由宽度推算。放在 Column 里会取列宽;放在 Row、StackBox 等不分配宽度的父容器里时要用布局选项(layout.W、layout.Grow)或 MaxWidth 提供宽度,否则盒子是 0×0。
  • 内容铺满。内容放在一个叠放容器中,Div、Column 这类容器会被拉伸到整个盒子。图片可通过 .Layout(layout.Abs(0, 0, 0, 0)).Fit("cover") 铺满盒子并保持原始比例,超出部分会被裁掉。
  • 状态:AspectRatio 没有内部状态,比例由调用方每帧传入,改变比例下一帧就生效。
  • 无障碍:A11yLabel(s) 把盒子暴露为带名称的分组(图片说明);纯装饰区域不设置即可。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

AspectRatio 宽高比

gallery 演示 AspectRatio 的具名 Render 函数,源码 page_aspect_ratio.go#L30。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.AspectRatio ​

go
func AspectRatio(c *Context, ratio float32, fn func(), opts ...layout.Option) *AspectRatioW

AspectRatio adds a ratio box (width / height, e.g. 16.0/9) and builds fn inside it, stretched over the whole box:

ui.AspectRatio(c, 16.0/9, func() { ui.Image(c, src).Fill() }).Radius(8)

In a column it spans the column's width; where nothing gives it a width (a row, a stack) it is zero wide unless MaxWidth or layout options set one. A ratio <= 0 is taken as 1.

返回 *AspectRatioW,可链式调用的方法见 AspectRatioW 方法。

AspectRatioW 方法 ​

ui.AspectRatioW:AspectRatioW is a container that keeps a fixed width-to-height ratio (shadcn/ui AspectRatio, CSS aspect-ratio): it takes the width its parent gives it and is ratio times less tall; the content fills it.

方法说明签名
A11yLabelA11yLabel exposes the box as a named group (a figure caption).A11yLabel(s string) *AspectRatioW
IDID returns the box ID.ID() ID
MaxWidthMaxWidth limits the width (and with it the height).MaxWidth(px float32) *AspectRatioW
RadiusRadius rounds the corners and clips the content to them.Radius(r float32) *AspectRatioW
RatioRatio returns the width-to-height ratio in effect.Ratio() float32
RectRect returns the box rectangle of the last frame.Rect() geom.Rect

相关链接 ​

以 MIT 许可发布