---
title: "#245: Masking Views with Text Using CATextLayer 📖"
url: https://littlebitesofcocoa.com/245-masking-views-with-text-using-catextlayer
author: Jake Marsh
date: 2016-07-12
published: 2016-07-12
topics: ["Core Graphics"]
description: "Core Graphics and Core Animation are incredibly powerful tools with lots of useful components. Today we'll try out one of them, CATextLayer, and learn how..."
---

# #245: Masking Views with Text Using CATextLayer 📖

By Jake Marsh, July 12, 2016. HTML: https://littlebitesofcocoa.com/245-masking-views-with-text-using-catextlayer

**Core Graphics** and **Core Animation** are incredibly powerful tools with lots of useful components. Today we'll try out one of them, _CATextLayer_, and learn how it can help us create some fun and interesting visuals. Let's get started.

First, we'll need a **view** to mask. We'll create a **UIImageView** and use this great looking Apple TV marketing image to test things out.

``` swift
let imageView = UIImageView(
  UIImage(named: “apple-tv-color-bars.png”)
)

imageView.sizeToFit()
```

<img src='https://media.littlebitesofcocoa.com/bite-assets/246-1.png' style='max-width: 399px;' />

Then, we'll need a _CATextLayer_. We'll configure it to so it has the same size as our **imageView**, and do some **Core Graphics** housekeeping:

``` swift
let textLayer = CATextLayer()

textLayer.frame = imageView.bounds
textLayer.rasterizationScale = UIScreen.mainScreen().scale
textLayer.contentsScale = UIScreen.mainScreen().scale
```

Next, we'll configure the **text layer** to look the way we want. We'll set a **font**, **alignment**, and **wrapping**/**truncation** settings:

``` swift
textLayer.alignmentMode = kCAAlignmentCenter
textLayer.fontSize = 42.0
textLayer.font = UIFont(name: "TrebuchetMS-Bold", size: 42.0)
textLayer.wrapped = true
textLayer.truncationMode = kCATruncationEnd
```

We'll give it some content to display:

``` swift
textLayer.string = "Little Bites of Cocoa"
```

Finally, we'll set the **text layer** as the **mask** of our **imageView's layer**.

``` swift
imageView.layer.mask = textLayer
```

<img src='https://media.littlebitesofcocoa.com/bite-assets/246-2.png' style='max-width: 399px;' />

Success!

Looking for a library that does all this for you (plus a bit more)? Check out [Translucid](https://github.com/Ekhoo/Translucid) by [Lucas Ortis](https://github.com/Ekhoo).
