---
title: "#99: Auto Layout Visual Format Language 🔤📐"
url: https://littlebitesofcocoa.com/99-auto-layout-visual-format-language
author: Jake Marsh
published: 2015-10-08
topics: ["Layout"]
description: "In Bite #98, we learned how to work with Auto Layout in code. While this helped us gain a ton of flexibility, our code is now a bit..."
---

# #99: Auto Layout Visual Format Language 🔤📐

By Jake Marsh, October 8, 2015. HTML: https://littlebitesofcocoa.com/99-auto-layout-visual-format-language

In [Bite #98][1], we learned how to work with **Auto Layout** in code. While this helped us gain a ton of flexibility, our code is now a bit... verbose. For simple layouts this won't be a big deal, but for anything complex we would quickly rack of hundreds of lines of code for just our layout. Today we'll look at one solution for this issue: **Auto Layout's** _Visual Format Language_. Let's take a look.

[1]: https://littlebitesofcocoa.com/98

Before jumping into code, let's look at the _Visual Format Language_ itself. It's essentially just a **string** that the system parses and turns into an **array** of **NSLayoutConstraint** objects:

``` swift
"|-[header]-|"
```

The two **pipes** on the outside represent the **superview**. The **dashes** represent **spacing**, in this case, the system-standard spacing. **Views** are described with a name in **brackets**.

When parsed, this would create 2 new **constraints**: 1 leading and 1 trailing. It would keep the header the standard **spacing** away from the left and right edges of the **superview**. To let the system know our **views** by name we put them in a **dictionary**:

``` swift
let views = [ "header": header ]
```

If we wanted to use a specific **spacing** amount we could describe that within **dashes** on the edges of our _Visual Format Language_:

``` swift
"|-20-[header]-20-|"
```

That's fine for simple **spacing**, but what if we needed to reference lots of numeric values? We can name those and put them in a **dictionary** just like our **views**:

``` swift
"|-edgeSpacing-[header]-edgeSpacing-|"
```

Now that we have a basic understanding, we can add our first set of **constraints**:

``` swift
container.addConstraints(
  NSLayoutConstraint.constraintsWithVisualFormat(
    "|-[header]-|",
    options: NSLayoutFormatOptions(rawValue: 0),
    metrics: nil,
    views: [ "header": header ]
  )
)
```

We can prepend a `H:` or `V:` to describe **constraints** along the horizontal or vertical **axes**. If we don't prepend either, horizontal is assumed.

We can use parenthesis to describe a view's width or height:

``` swift
"V:|[header(180)]"
```

Let's add 2 more **constraints** to pin our header to the top, and give it a fixed height:


``` swift
container.addConstraints(
  NSLayoutConstraint.constraintsWithVisualFormat(
    "V:|[header(headerHeight)]",
    options: NSLayoutFormatOptions(rawValue: 0),
    metrics: [ "headerHeight": 180.0 ],
    views: [ "header": header ]
  )
)
```

We've only scratched the surface of what's possible with _Visual Format Language_. In the future we'll look at configuring inequalities, **constraint** priorities, and more.
