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.
| Case | Light | Dark | Notes | ||
|---|---|---|---|---|---|
primary | #1d1d1f | #f5f5f7 | The default text color. | ||
secondary | #6e6e73 | #98989d | Less important text. | ||
gray | #8e8e93 | #8e8e93 | |||
red | #ff3b30 | #ff453a | |||
orange | #ff9500 | #ff9f0a | |||
yellow | #ffcc00 | #ffd60a | |||
green | #34c759 | #30d158 | Also the color of a toggle that’s on. | ||
blue | #007aff | #0a84ff | Also the accent color: button labels, the text caret, the selected tab. | ||
purple | #af52de | #bf5af2 | |||
pink | #ff2d55 | #ff375f | |||
white | #ffffff | #ffffff | The same in both appearances. | ||
black | #000000 | #000000 | The same in both appearances. | ||
clear | transparent | transparent | Draws nothing. | ||
lightGray | #e5e5ea | #3a3a3c | Subtle backgrounds, hover highlights. | ||
sidebar | #f3f3f6 | #252527 | Side panels; also the tab bar of a TabView. | ||
highlight | #d6e4fc | #223e68 | A selected row. | ||
separator | #d8d8de | #3e3e42 | Thin lines; also the color of a Divider. | ||
background | #ffffff | #1c1c1e | The window’s background. | ||
rgb(red, green, blue) | yours | yours | A color of your own. Each value is an Int from 0 to 255. | ||
rgba(red, green, blue, opacity) | yours | yours | The 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)rgbandrgbatake 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. Besidesrgba,.opacityfades a whole view. - Printing a color gives its case’s name:
red, orrgbfor 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)Weight
Section titled “Weight”Used by .font(weight:) and g.text(weight:).
| Case | Font weight |
|---|---|
thin | 100 |
ultraLight | 200 |
light | 300 |
regular | 400, the default |
medium | 500 |
semibold | 600 |
bold | 700 |
heavy | 800 |
black | 900 |
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)Alignment
Section titled “Alignment”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.
| Case | Horizontal | Vertical |
|---|---|---|
topLeading | leading | top |
top | center | top |
topTrailing | trailing | top |
leading | leading | center |
center | center | center |
trailing | trailing | center |
bottomLeading | leading | bottom |
bottom | center | bottom |
bottomTrailing | trailing | bottom |
Where it’s used decides which parts count:
| Used in | Default | What counts |
|---|---|---|
ZStack(alignment:) | .center | Both parts: where each layer sits in the stack. |
.frame(alignment:) | see frame | Both parts: where the content sits in a larger frame. |
VStack(alignment:) | .center | Only the horizontal part: how the views line up across the stack. .topLeading acts like .leading, and .top and .bottom like .center. |
HStack(alignment:) | .center | Only the vertical part: .topLeading acts like .top, .bottomTrailing like .bottom. .leading and .trailing act like .top and .bottom. |
.textAlignment(…) | .leading | Only 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.
Appearance
Section titled “Appearance”Used by the appearance: of app.
| Case | The app looks… |
|---|---|
system | light or dark as the system does, and changes with it. The default. |
light | always light. |
dark | always dark. |
app Player(appearance: .dark) { Text("Now playing")}isDarkMode() tells which one the app is
showing in.
CursorStyle
Section titled “CursorStyle”Used by .cursor: the pointer shown over a
view.
| Case | The pointer |
|---|---|
arrow | The usual arrow. |
hand | A pointing hand, for links. |
crosshair | A cross, for picking a point or drawing. |
grab, grabbing | An open hand, and a closed one, for things that are moved. |
text | The I-beam of text. |
move | Arrows in four directions. |
wait | Busy. |
notAllowed | A crossed circle. |
resizeHorizontal, resizeVertical | Arrows sideways, and up and down. |
zoomIn, zoomOut | A magnifying glass with a plus or a minus. |
BlendMode
Section titled “BlendMode”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.
| Case | The result |
|---|---|
normal | The layer’s colors, over what’s under it (the default). |
multiply | The two multiplied: darker. White changes nothing. |
screen | The opposite of multiply: lighter. Black changes nothing. |
overlay | Multiply where what’s under is dark, screen where it’s light. |
darken, lighten | The darker, or the lighter, of the two. |
colorDodge, colorBurn | What’s under brightened, or darkened, by the layer. |
hardLight, softLight | Multiply or screen depending on the layer’s colors: a harsh light, and a soft one. |
difference | The lighter minus the darker. White inverts; black changes nothing. |
exclusion | Like difference, with less contrast. |
hue, saturation, color, luminosity | One 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).
RibbonSize
Section titled “RibbonSize”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.
PointerEvent
Section titled “PointerEvent”What .onPointer tells its block. A
struct with these fields:
| Field | Type | |
|---|---|---|
kind | PointerKind | What happened (below). |
x, y | Float | Where 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, windowY | Float | Where 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, dy | Float | For .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). |
button | PointerButton | .left, .right or .middle, for .down, .drag and .up. |
clicks | Int | For .down and .up: 1 for a click, 2 for the second click of a double click, and so on. |
shift, alt, command | Bool | Whether 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:
| Case | When |
|---|---|
down | A button was pressed on the view. |
drag | The mouse moved with that button held, on the view or off it. |
up | The button was let go. |
move | The mouse moved over the view with no button held. |
leave | The mouse left the view (or the window). |
scroll | The mouse wheel, or two fingers on a trackpad. |
pinch | Two 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) } _ -> {} }}KeyEvent
Section titled “KeyEvent”What .onKeyEvent tells its block. A
struct with these fields:
| Field | Type | |
|---|---|---|
key | String | The 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". |
text | String | What the key types ("A" for Shift+A), or "" for a key that types nothing. |
shift, alt, command, control | Bool | Whether 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. |
repeated | Bool | True for the presses a key held down sends after the first one. |
up | Bool | True 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() }}