---
title: "Text bites - Little Bites of Cocoa"
description: "Little Bites of Cocoa about Text: short tips and techniques for iOS and Mac development, each a brief explanation of one concept, technique or tool."
url: "https://littlebitesofcocoa.com/topics/25-text"
author: "Jake Marsh"
---
# Bites about: [Text](https://littlebitesofcocoa.com/topics/25-text)

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

## [#309: UIFontMetrics 📐](https://littlebitesofcocoa.com/309-uifontmetrics)

 Tuesday June 6th, 2017

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

Happy **WWDC 2017**! Today we're beginning our look at the incredibly large list of updates and improvements announced this week with *`UIFontMetrics`*. Let's jump in.

We'll start with the problem we're trying to solve. It's all about [Dynamic Type](https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/).

Our users can adjust their preferred **Dynamic Type** value in **Settings.app** to display the text in our **apps** larger and more prominently.

This works great when we want to use the system default font, since we can simply write some code like this:

```swift
let font = UIFont.preferredFont(forTextStyle: .headline)
```

This will return a `UIFont` that is appropriate for the given **text style**, adjusted by size and weight to match the user's **Dynamic Type** setting.

But what about custom fonts? In the past this was a bit cumbersome and we often had to resort to ugly hacks.

Enter *`UIFontMetrics`*! We can use this new type in **iOS 11** to ask the system to scale our font size for us:

```swift
let headlineMetrics = UIFontMetrics(forTextStyle: .headline)
let fontBeforeScaling = UIFont(name: "Chicago", size: 16.0)
let font = headlineMetrics.scaledFont(for: fontBeforeScaling)
```

Neat!

There's always one more fun trick tucked away in ***UIFontMetrics***, and that is scaling arbitrary values. This is great for helping us size our UI elements (for example buttons or headers) to accomodate dynamically sized fonts that live inside:

```swift
let headlineMetrics = UIFontMetrics(forTextStyle: .headline)
let heightBeforeScaling = 44.0
let height = headlineMetrics.scaledValue(forValue: heightBeforeScaling)
```

No more large text in tiny buttons. Very cool!

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

## [#280: Flowing Text Around Images With Exclusion Paths 🖊](https://littlebitesofcocoa.com/280-flowing-text-around-images-with-exclusion-paths)

 Tuesday October 25th, 2016

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

*Text* is a huge part of **iOS apps**. Today we'll look at one way we can polish up the **text** in our **apps**, by allowing it to naturally "flow" around objects. Let's begin.

We'll start with a "before" picture. Here we've got a basic setup with some **text** and an **image** in the top right corner. Nothing too fancy here, just a regular *UITextView* and **UIImageView**:

```swift
let imageView = UIImageView(image: UIImage(named: "steve"))

let textView = UITextView(frame: .zero)
textView.text = "Here's to the crazy ones..."
```

![Image 1 of 4 from bite #280: Flowing Text Around Images With Exclusion Paths](https://media.littlebitesofcocoa.com/280-1.png)

This looks fine. But our **text** is needlessly narrow. It'd be great if we could make the **text** *expand* to the full width of the device. We'll change our *Text View's* layout so it does:

![Image 2 of 4 from bite #280: Flowing Text Around Images With Exclusion Paths](https://media.littlebitesofcocoa.com/280-2.png)

Well, we're getting closer. Now we just need a way to make the **text** *"flow"* or *"wrap"* around our image. We'll start by getting the **frame** of the **image view**, then we'll create a **path** from it:

```swift
let imagePath = UIBezierPath(rect: imageView.frame)
```

Finally, we'll set our new path as one of the `exclusionPaths` of our *Text View's* `textContainer`:

```swift
textView.textContainer.exclusionPaths = [imagePath]
```

![Image 3 of 4 from bite #280: Flowing Text Around Images With Exclusion Paths](https://media.littlebitesofcocoa.com/280-3.png)

Success! Since `exclusionPaths` is an **Array**, we can flow **text** around as many **shapes** as we want!

![Image 4 of 4 from bite #280: Flowing Text Around Images With Exclusion Paths](https://media.littlebitesofcocoa.com/280-4.png)

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)
- [B-sides](https://littlebitesofcocoa.com/topics/80-b-sides)

## [#246: UITextField B-sides 📼](https://littlebitesofcocoa.com/246-uitextfield-b-sides)

 Wednesday July 13th, 2016

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)
- [B-sides](https://littlebitesofcocoa.com/topics/80-b-sides)

**UITextField** is one of the sort of "low key" hidden gems of **UIKit**. Today we'll look at some of the lesser-known things it can do and how to configure them. Let's dive in.

We'll begin by creating a **text field** and customizing its look and feel. First, instead of boring plain **placeholder**, let's spice things up a bit:

```swift
let placeholder = NSMutableAttributedString()

placeholder.append(
  AttributedString(
    string: "Spaceship ",
    attributes: [NSFontAttributeName : boldFont]
  )
)

placeholder.append(
  AttributedString(
    string: "Name",
    attributes: [NSFontAttributeName : font]
  )
)

textField.attributedPlaceholder = placeholder
```

![Image 1 of 2 from bite #246: UITextField B-sides ](https://media.littlebitesofcocoa.com/bite-assets/246-example-1.png)

Nice, now let's customize how our **text field** works.

Suppose we wanted to clear out its contents when a user tapped it. We simply set `clearsOnBeginEditing` to `true`.

Similarly, if we wanted to clear the contents not when the user tapped the **field**, but rather when the user began typing content, we can set `clearsOnInsertion` to `true`.

Let's add some more "padding" to our **text field**. This turns out to be a tricker than expected.

*UITextField* allows for customization of the **rects** it uses to draw text via subclassing:

```swift
class PaddedTextField : UITextField {
  override func textRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.insetBy(dx: 16.0, dy: 8.0)
  }
}
```

Finally, we'd like allow users to fancy up (**bold**, *italic*, underline) the names of their spaceships. Turns out *UITextField* makes this super easy:

```swift
textField.allowsEditingTextAttributes = true
```

With **attribute editing** enabled, we now get this great editing rich-text UI for free. Neat!

![Image 2 of 2 from bite #246: UITextField B-sides ](https://media.littlebitesofcocoa.com/bite-assets/246-example-2.png)

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)
- [Core Graphics](https://littlebitesofcocoa.com/topics/33-core-graphics)

## [#144: Drawing Multiline Strings 📖](https://littlebitesofcocoa.com/144-drawing-multiline-strings)

 Thursday December 10th, 2015

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)
- [Core Graphics](https://littlebitesofcocoa.com/topics/33-core-graphics)

Let's draw some *strings*! There's plenty of text-drawing capabilities in **iOS** and **OS X**, with full frameworks like **Text Kit** (or the lower-level **Core Text**) dedicated to the task. Today, we'll start looking at these capabilities by using **Core Graphics** and **UIKit** to draw a *multiline string*.

Sometimes we need to draw text ourselves, “manually”. This can be helpful when optimizing for scrolling performance, or when complete customization is necessary.

First we'll need a view to draw into, we'll make a new **UIView**, and override `drawRect`.

Next, we'll start implementing our `drawRect` **function** with a few **variables**. We'll choose a font, then setup a **paragraph style** to make our text multiline, with some tall lines.

We'll also setup some **drawing options** to tell the system we want multiline text:

```swift
let textFont = UIFont(name: "Avenir Next", size: 17.0)!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.2
paragraphStyle.lineBreakMode = .ByWordWrapping

let drawingOptions: NSStringDrawingOptions = [
  .UsesLineFragmentOrigin, .UsesFontLeading]
```

We'll save these off into a `textAttributes` **dictionary**. Next, we'll need to figure out what **rectangle** we'll be drawing our **text** into.

We'll use the `boundingRectWithSize` **function** to find out how tall it will be when constrained to the **width** of our **view**:

```swift
let textRect = (text as NSString)
  .boundingRectWithSize(
    CGSizeMake(bounds.size.width, CGFloat.max),
    options: drawingOptions,
    attributes: textAttributes,
    context: nil)
```

Then we'll just pass that calculated **textRect** into a call to **drawWithRect**:

```swift
(text as NSString).drawWithRect(textRect,
  options: drawingOptions,
  attributes: textAttributes,
  context: nil)
```

![Image from bite #144: Drawing Multiline Strings](https://media.littlebitesofcocoa.com/bite-assets/144-drawing-multiline-strings-final.png)

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

## [#54: Dynamic Type Basics 📄](https://littlebitesofcocoa.com/54-dynamic-type-basics)

 Thursday August 6th, 2015

Topics

- [UIKit](https://littlebitesofcocoa.com/topics/1-uikit)
- [Text](https://littlebitesofcocoa.com/topics/25-text)

Dynamic Type is a system introduced back in iOS 7 with the intention of unifying and simplifying how users change their preference for how big text should be on the screen. While the user choice is simple, under the hood Dynamic Type earns its name by dynamically adjusting things such as spacing between characters and character weights to make text as readable as possible.

Let’s take a look at how to add simple Dynamic Type support.

## Text Styles

The main way you interact with Dynamic Type is through the built-in set of text styles. As of **iOS 9**, 9 styles are available to choose from:

- `UIFontTextStyleTitle1`
- `UIFontTextStyleTitle2`
- `UIFontTextStyleTitle3`
- `UIFontTextStyleHeadline`
- `UIFontTextStyleSubheadline`
- `UIFontTextStyleBody`
- `UIFontTextStyleFootnote`
- `UIFontTextStyleCaption1`
- `UIFontTextStyleCaption2`

## Retrieving Fonts

If you’re using the system font, all you need is:

```swift
UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
```

For custom fonts, you’ll need to grab a font descriptor first, then create the UIFont manually:

```swift
let fontSize = UIFontDescriptor
  .preferredFontDescriptorWithTextStyle(UIFontTextStyleHeadline)
  .pointSize

let font = UIFont(name: "Avenir Next", size: fontSize)
```

Lastly, you'll want to update your UI when the user changes their preferences in Settings.app. Observe this notification and trigger a re-layout when it occurs:

```swift
NSNotificationCenter.defaultCenter().addObserver(self,
  selector: "preferredContentSizeChanged:",
  name: UIContentSizeCategoryDidChangeNotification, object: nil)
```

 

 
