Canvas & Layout

Pins and resizing

A design is not finished at one size. Phones differ, windows resize, a translated label is half as long again. Nox gives you two ways to make a frame hold: auto layout, where the frame places its children, and pins, where each child of a free frame says which edges it holds to. This article is about pins and the limits that go with them.

A card as designed, and after its frame is made wider and taller: each child holds to the edges it is pinned to
A card as designed, and after its frame is made wider and taller: each child holds to the edges it is pinned to

Pins#

Pins apply to a child of a frame without a layout (the inspector calls that Free). When the frame is resized, each child follows its pins, one along the width and one along the height.

PinIn the toolsWhen the frame grows
Left / Topleft, topThe child stays where it is. This is the default.
Right / Bottomright, bottomIt keeps its distance from the far edge.
Left and right / Top and bottombothIt keeps both distances, so it stretches.
CentercenterIt keeps its distance from the middle.
ScalescaleIts place and its size grow in proportion to the frame.

In the card above, the badge is pinned right, the caption bottom, the track left and right and bottom, the round mark center both ways. The blue bar inside the track is pinned to scale, so it stays at 64% of the track.

By hand
  1. Select a child of a frame whose Layout is Free.
  2. In the inspector's Pins section, choose H: Left, Right, Left and right, Center or Scale.
  3. Choose V: Top, Bottom, Top and bottom, Center or Scale. The small picture lights the edges it holds to.
  4. Drag the frame's handles and watch it follow.
The Pins section of the inspector, with a child pinned right and top
The Pins section of the inspector, with a child pinned right and top
With an agent
Pin the Pro badge to the top-right of the card and make the progress track stretch with the card's width, staying 28 from the bottom. Then make the card 340 wide.

The agent calls update_nodes. Pins are followed when a frame's width or height changes by hand or through the tools:

{
  "changes": [
    {"id": "BADGE_ID", "pin": {"horizontal": "right", "vertical": "top"}},
    {"id": "TRACK_ID", "pin": {"horizontal": "both", "vertical": "bottom"}},
    {"id": "CARD_ID", "width": 340}
  ]
}

Two things to know:

  • Pins do not apply to children in the flow of an auto layout. The layout places those. A free child of a layout frame can carry pins.
  • A frame whose size a layout decides (it hugs or fills) does not move its pinned children. Pins answer a resize you or an agent make.

Stretch, scale and keep proportions#

One picture in a frame made wider: left as it is, stretched by both pins, scaled, and stretched while keeping its proportions
One picture in a frame made wider: left as it is, stretched by both pins, scaled, and stretched while keeping its proportions

Left and right keeps the margins and lets the proportions go. That is right for a bar or a divider and wrong for a photo or an icon.

Scale keeps the layer at the same part of the frame. Its margins grow with it.

Keep proportions fixes a layer's width to its height. Pinned to both edges both ways, it becomes as large as fits in its own shape. Use it for icons, avatars, portraits and artwork, and for a game's buttons across screens of other shapes.

By hand, open Game UI in the inspector and switch on Keep proportions. For an agent it is keep_ratio: true. With it set, a width given without a height takes the height along.

{"nodes": [{"kind": "frame", "name": "Stage", "x": 0, "y": 0, "width": 160, "height": 120, "fill": "#FFFFFF", "children": [
  {"kind": "rectangle", "name": "Picture", "x": 20, "y": 20, "width": 120, "height": 80, "corner_radius": 10, "fill": "#6E9BFF",
   "pin": {"horizontal": "both", "vertical": "both"}, "keep_ratio": true}]}]}

Min and max size#

Limits say how far a layout may push a size. min_size and max_size are [width, height]; 0 means no limit on that axis. They apply to a child that fills and to a frame or text that hugs. A fixed size is not limited.

A chat bubble that hugs its text up to 220 wide, and a search field that fills between 120 and 180
A chat bubble that hugs its text up to 220 wide, and a search field that fills between 120 and 180
  • A text with width: "hug" and max_size: [220, 0] grows to 220 and then wraps.
  • A field with width: "fill", min_size: [120, 0] and max_size: [180, 0] takes what its row leaves, between those two widths.

By hand they are Min W, Min H, Max W and Max H under More, below the size fields. An agent sets them like any property:

{"nodes": [{"kind": "frame", "name": "Bubble", "x": 0, "y": 0, "width": "hug", "height": "hug", "fill": "#FFFFFF", "corner_radius": 14,
  "layout": {"direction": "vertical", "padding": [8, 12]},
  "children": [{"kind": "text", "text": "Running ten minutes late. Save me a seat near the window if you can.", "width": "hug", "max_size": [220, 0], "font_size": 14, "line_height": 20, "fill": "#111318"}]}]}

Text that shrinks to fit#

A button is 140 wide. "Play" fits at 18. "Продолжить игру" does not. You can cut the text, or let it shrink.

Three labels in buttons of one size: cut with an ellipsis, and shrunk to fit
Three labels in buttons of one size: cut with an ellipsis, and shrunk to fit

A text with a width and a height of its own can be given a least font size. Where its lines do not fit its box at its size, it is shown smaller, in half units, until they fit, and never smaller than the least. The font size you set does not change, so a short label stays large.

By hand
  1. Give the text a fixed box: a width, and Fixed height under More in the Text section.
  2. In the same place, set Shrink to fit, least to the smallest size you accept, such as 10.
With an agent
Make the button labels shrink to fit their buttons, never below 10 pt.

The agent calls update_nodes on each label, which must have a width and a height of its own:

{"changes": [{"id": "LABEL_ID", "width": 116, "height": 24, "text_fit": 10, "text_align": "center", "text_vertical_align": "middle"}]}
Note

Set the least size no lower than you would accept on a real screen: 10 or 11 for a label, 12 for anything read at length. What still does not fit is cut if the text is set to be cut.

How a design holds at other sizes#

A short checklist before you call a screen done:

  1. Structure with auto layout. Screens are columns; bars are rows with space between; content fills.
  2. Pins for what floats. A close button pinned right and top, a floating button pinned right and bottom, a backdrop pinned to all four edges.
  3. Limits where it matters. A least width on cards and fields, a most width on paragraphs (about 560 for body text).
  4. Text that copes. Auto height for paragraphs, cut or shrink for labels in fixed boxes.
  5. Try it. Resize the frame to 320, 390 and 430 wide for a phone, or 1024 and 1440 for a window, and look.

An agent can do step 5 for you: ask it to duplicate the screen at other widths and check each one. Worked examples are in Responsive patterns.