Skip to content

Colors, weights and alignment

Tessel has four small built-in enums for styling: Color, Weight, Alignment and Appearance. Like any enum, you usually write a case with a leading dot where the type is known, as in .color(.red), and you can use the types in your own code:

view Badge(label: String, tint: Color = .blue, align: Alignment = .center) {
Text(label)
.color(.white)
.padding(horizontal: 8, vertical: 2)
.background(tint, radius: 8)
.frame(width: 120, alignment: align)
}

(Another built-in enum, IconName, is listed on the Icons page.)

Used by .color, .background, .border and .hoverBackground.

CaseLightDarkNotes
primary#1d1d1f#f5f5f7The default text color.
secondary#6e6e73#98989dLess important text.
gray#8e8e93#8e8e93
red#ff3b30#ff453a
orange#ff9500#ff9f0a
yellow#ffcc00#ffd60a
green#34c759#30d158Also the color of a toggle that’s on.
blue#007aff#0a84ffAlso the accent color: button labels, the text caret, the selected tab.
purple#af52de#bf5af2
pink#ff2d55#ff375f
white#ffffff#ffffffThe same in both appearances.
black#000000#000000The same in both appearances.
cleartransparenttransparentDraws nothing.
lightGray#e5e5ea#3a3a3cSubtle backgrounds, hover highlights.
sidebar#f3f3f6#252527Side panels; also the tab bar of a TabView.
highlight#d6e4fc#223e68A selected row.
separator#d8d8de#3e3e42Thin lines; also the color of a Divider.
background#ffffff#1c1c1eThe window’s background.
rgb(red, green, blue)yoursyoursA color of your own. Each value is an Int from 0 to 255.
rgba(red, green, blue, opacity)yoursyoursThe same, see-through: opacity is a Float from 0 (invisible) to 1 (solid).

Each named color has a value for the light and for the dark appearance, and the app uses the one for how it’s showing (see Light and dark). white, black and the colors you make with rgb and rgba are the same in both.

let brand: Color = .rgb(255, 136, 0)
Text("Sale").background(brand, radius: 6)
Text("Note").background(.rgba(255, 136, 0, 0.2), radius: 6)
  • rgb and rgba take their values without labels. Values outside their range are brought into it: .rgb(300, -5, 0) is .rgb(255, 0, 0).
  • The named colors are fully opaque except clear. Besides rgba, .opacity fades a whole view.
  • Printing a color gives its case’s name: red, or rgb for one of your own.

A typical sidebar row, using several of them:

HStack {
Icon(.fileText).color(.secondary)
Text("main.tsl")
Spacer()
}
.padding(horizontal: 8, vertical: 4)
.background(.highlight, radius: 4)

Used by .font(weight:) and g.text(weight:).

CaseFont weight
thin100
ultraLight200
light300
regular400, the default
medium500
semibold600
bold700
heavy800
black900

The family’s face nearest the weight is used: a family without a light one is drawn regular. fontStyles tells which faces a family has.

Text("Heading").font(size: 20, weight: .semibold)

Used by the alignment: of VStack, HStack, ZStack and .frame, and by .textAlignment.

An alignment names a position in a box: a horizontal part (leading, center or trailing) and a vertical part (top, center or bottom). “Leading” is the left side and “trailing” the right side.

CaseHorizontalVertical
topLeadingleadingtop
topcentertop
topTrailingtrailingtop
leadingleadingcenter
centercentercenter
trailingtrailingcenter
bottomLeadingleadingbottom
bottomcenterbottom
bottomTrailingtrailingbottom

Where it’s used decides which parts count:

Used inDefaultWhat counts
ZStack(alignment:).centerBoth parts: where each layer sits in the stack.
.frame(alignment:)see frameBoth parts: where the content sits in a larger frame.
VStack(alignment:).centerOnly the horizontal part: how the views line up across the stack. .topLeading acts like .leading, and .top and .bottom like .center.
HStack(alignment:).centerOnly the vertical part: .topLeading acts like .top, .bottomTrailing like .bottom. .leading and .trailing act like .top and .bottom.
.textAlignment(…).leadingOnly the horizontal part: how the lines of a text line up.
VStack(alignment: .leading) {
Text("Name").font(weight: .bold)
Text("Ada Lovelace")
}
ZStack(alignment: .bottomTrailing) {
Text("Photo").frame(width: 120, height: 80).background(.lightGray)
Text("new").font(size: 10).padding(4)
}

See Alignment for a guided tour with pictures.

Used by the appearance: of app.

CaseThe app looks…
systemlight or dark as the system does, and changes with it. The default.
lightalways light.
darkalways dark.
app Player(appearance: .dark) {
Text("Now playing")
}

isDarkMode() tells which one the app is showing in.

Used by .cursor: the pointer shown over a view.

CaseThe pointer
arrowThe usual arrow.
handA pointing hand, for links.
crosshairA cross, for picking a point or drawing.
grab, grabbingAn open hand, and a closed one, for things that are moved.
textThe I-beam of text.
moveArrows in four directions.
waitBusy.
notAllowedA crossed circle.
resizeHorizontal, resizeVerticalArrows sideways, and up and down.
zoomIn, zoomOutA magnifying glass with a plus or a minus.

Used by g.beginLayer: how a layer’s colors mix with the colors under it. The names and the arithmetic are the ones photo editors and CSS use.

CaseThe result
normalThe layer’s colors, over what’s under it (the default).
multiplyThe two multiplied: darker. White changes nothing.
screenThe opposite of multiply: lighter. Black changes nothing.
overlayMultiply where what’s under is dark, screen where it’s light.
darken, lightenThe darker, or the lighter, of the two.
colorDodge, colorBurnWhat’s under brightened, or darkened, by the layer.
hardLight, softLightMultiply or screen depending on the layer’s colors: a harsh light, and a soft one.
differenceThe lighter minus the darker. White inverts; black changes nothing.
exclusionLike difference, with less contrast.
hue, saturation, color, luminosityOne part of the layer’s color (its hue; its saturation; its hue and saturation; its lightness), with the rest from what’s under it.

A side of a view, for .popover(edge:): top, bottom, leading (the left) and trailing (the right).

How big a ribbon’s button, toggle or menu is: small (its icon next to its label; the default) or large (its icon over its label, as tall as its group).

How an .animation moves from one state to the next: easeInOut (slow, fast, slow; the default), easeIn, easeOut, linear, spring (a little past the end, and back) and bouncy (further past, and back and forth). See How a change moves.

What .onPointer tells its block. A struct with these fields:

FieldType
kindPointerKindWhat happened (below).
x, yFloatWhere the mouse is, in points from the view’s top left corner (inside its padding). During a drag they can be outside the view, and negative.
windowX, windowYFloatWhere the mouse is, in points from the window’s top left corner: for placing a popup of your own, or comparing with other views.
dx, dyFloatFor .move and .drag: how far the mouse moved since the event before. For .scroll: how far to scroll, across and down (positive dy: towards what’s further down). For .pinch: dy is how much larger (0.1: a tenth larger; negative: smaller).
buttonPointerButton.left, .right or .middle, for .down, .drag and .up.
clicksIntFor .down and .up: 1 for a click, 2 for the second click of a double click, and so on.
shift, alt, commandBoolWhether the key was held. command is Cmd on macOS and Ctrl on Windows; alt is Option on macOS.

PointerKind has the cases down, up, move, drag, scroll, pinch and leave:

CaseWhen
downA button was pressed on the view.
dragThe mouse moved with that button held, on the view or off it.
upThe button was let go.
moveThe mouse moved over the view with no button held.
leaveThe mouse left the view (or the window).
scrollThe mouse wheel, or two fingers on a trackpad.
pinchTwo fingers pinched on a trackpad (macOS only).
.onPointer { e in
match e.kind {
.down -> { start = (e.x, e.y) }
.drag -> { offset = (offset.0 + e.dx, offset.1 + e.dy) }
.scroll -> { zoom = zoom * exp(-e.dy * 0.01) }
_ -> {}
}
}

What .onKeyEvent tells its block. A struct with these fields:

FieldType
keyStringThe key, without the modifiers: "a" for Shift+A, "1" for Shift+1, or a key’s name: "enter", "escape", "tab", "space", "backspace", "delete", "left", "right", "up", "down", "home", "end", "pageup", "pagedown", "insert", "f1" … "f12".
textStringWhat the key types ("A" for Shift+A), or "" for a key that types nothing.
shift, alt, command, controlBoolWhether the key was held. command is Cmd on macOS and Ctrl on Windows and Linux; control is the Ctrl key on every system; alt is Option on macOS.
repeatedBoolTrue for the presses a key held down sends after the first one.
upBoolTrue when the key was let go. Only with .onKeyEvent(releases: true).
.onKeyEvent { e in
if e.key == "delete" && !e.repeated {
deleteSelected()
} else if e.key == "a" && e.command {
selectAll()
}
}