Reader small image

You're reading from  SwiftUI Cookbook - Third Edition

Product typeBook
Published inDec 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805121732
Edition3rd Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Juan C. Catalan
Juan C. Catalan
author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan

Right arrow

SwiftUI Tips and Tricks

In the previous chapters, we tried to solve different problems in different recipes, grouping them together within a common theme. However, in this chapter, the recipes are not connected, apart from the fact that they are solutions for real-world problems.

We’ll start by exploring how we can test SwiftUI views using XCTest, which is Apple’s official framework for implementing automated tests.

SwiftUI provides a variety of built-in fonts that we can use in most of our apps but, sometimes, we may want more customization. So, we’ll see how to use custom fonts in SwiftUI.

Sometimes we must show some kind of documentation in the app, so we’ll see how to present Portable Document Format (PDF) documents.

Finally, we’ll implement a Markdown editor that shows an attributed preview while adding text with Markdown tags.

In this chapter, we will cover different topics in SwiftUI and real-world problems that you are...

Technical requirements

The code in this chapter is based on Xcode 15.0 and iOS 17.0. You can download and install the latest version of Xcode from the App Store. You’ll also need to be running macOS Ventura (13.5) or newer.

Simply search for Xcode in the App Store and select and download the latest version. Launch Xcode and follow any additional installation instructions that your system may prompt you with. Once Xcode has fully launched, you’re ready to go.

All the code examples for this chapter can be found on GitHub at https://github.com/PacktPublishing/SwiftUI-Cookbook-3rd-Edition/tree/main/Chapter17-SwiftUI-Tips-and-Tricks.

Using XCTest to test SwiftUI apps

When we build an app and implement the different features, we need to test that the app works correctly. Testing an app manually is a repetitive and time-consuming process, which becomes very boring. It is very common to make mistakes when performing boring and repetitive tasks. These mistakes could lead to the release of an app with defects to the App Store, affecting our users. Luckily, with the help of test automation, we can make Xcode test the app for us. Test automation is a very important discipline in software engineering and is based on the use of automated tests, with different levels of abstraction. The automated tests verify that the app under test works according to a well-known specification. Automated tests are a part of the software development process, and they verify that the code changes did not inadvertently introduce defects. With adequate automated test coverage, we have a higher confidence level, and we can catch the code defects...

Using custom fonts in SwiftUI

iOS comes with many preinstalled fonts that can be safely used in SwiftUI. However, the design of the app sometimes needs some custom fonts because they might be a part of the brand of the app maker.

In this recipe, we’ll see how to import a couple of custom fonts for building a menu page for an Italian restaurant.

Getting ready

  1. Create a SwiftUI app called RestaurantMenu.
  2. Get the Sacramento, Oleo Script Regular, and Oleo Script Bold custom fonts, which you can find in the repository at https://github.com/PacktPublishing/SwiftUI-Cookbook-3rd-Edition/tree/main/Resources/Chapter17/recipe2.
  3. Drag the font files to the Xcode project navigator pane to add the fonts as resources to the project, as shown in the following figure:

Figure 17.8: Custom fonts added to the project

  1. Make sure the Copy items if needed and RestaurantMenu options are checked:

Figure 17.9: Adding custom font files...

Showing a PDF in SwiftUI

Since iOS 11, Apple has provided PDFKit, a robust framework to display and manipulate PDF documents in your applications.

As you can imagine, PDFKit is based on UIKit. However, in this recipe, we’ll see how easy it is to integrate it with SwiftUI.

Getting ready

Let’s create a new SwiftUI app in Xcode called PDFReader.

For this, we need a PDF document to present. We have provided a sample PDF document in the repository but feel free to use the PDF document of your choice:

https://github.com/PacktPublishing/SwiftUI-Cookbook-3rd-Edition/blob/main/Resources/Chapter17/recipe3/PDFBook.pdf

Copy the PDF document into the project:

Figure 17.14: PDF document in the Xcode project

With the PDF document added as a resource to the project, we are ready to implement our PDF viewer in SwiftUI.

How to do it…

PDFKit provides a class called PDFView to render a PDF document.

Because the PDFView class is a subclass...

Implementing a Markdown editor with preview functionality

Markdown is a lightweight markup language to create rich text using a text editor. In recent years, it has become ubiquitous: you can use it in GitHub, when asking and replying to questions in Stack Overflow, for messaging in Discord, and more.

SwiftUI provides support for Markdown tags in the Text views.

In this recipe, we’ll experiment with Markdown, implementing a simple text editor.

Getting ready

This recipe doesn’t require any preliminary operations. Create a new Xcode project called MarkdownEditor.

How to do it…

Taking inspiration from the GitHub pull request comment textbox, we are going to implement an app with two tabs: a text editor view and a preview view, where the text is rendered as rich text, with bold, italics, and so on.

The text editor view will have a tab bar above the editor itself for adding Markdown tags for the bold, italics, strikethrough, and code formats...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
SwiftUI Cookbook - Third Edition
Published in: Dec 2023Publisher: PacktISBN-13: 9781805121732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan