This subchapter is provided as a free sample for SwiftUI Fundamentals book.
You can read this subchapter online or download the sample bundle with PDF and EPUB by clicking the link below.
Download free sampleTo get access to the contents of the whole book you need to purchase a copy.
FREE SAMPLE - online version
Text formatting
SwiftUI makes it simple to format and present a wide range of dynamic content in Text
views. Whether displaying lists, dates, measurements, or other types of information, SwiftUI provides flexible formatting options that adapt naturally to the user’s locale and context. This ensures that text remains clear, readable, and appropriate across different regions and languages.
# Format styles
SwiftUI’s Text
views provide flexible options for formatting data using the format parameter, which takes a FormatStyle
. This enables us to present structured data directly within a text view, adapting its appearance to suit different contexts and locales.
For instance, an array can be formatted as a grammatically correct list.
let genres = ["action", "comedy", "drama"]
Text("Genres: \(genres, format: .list(type: .and))")


The list format style will adjust automatically when the array is modified, ensuring the text is updated appropriately as items are added or removed.
Measurements, such as distances or dimensions, can be displayed in a format appropriate for the user’s locale. SwiftUI handles unit conversions seamlessly, presenting values in units familiar to the user.
let screenWidth = Measurement(
value: 18, unit: UnitLength.meters
)
Text("""
Screen width: \(
screenWidth,
format: .measurement(width: .wide)
)
""")


Numeric values, such as prices or totals, can be formatted to include a currency symbol and displayed in the appropriate monetary style.
let price = 15.99
Text("""
Ticket price: \(price, format: .currency(code: "USD"))
""")


By using FormatStyle
, we can manage how data is presented in Text
views with clarity and precision, tailoring the output to suit different requirements and regional conventions.
# Dynamic dates
Text
view allows us to display dates and times that update dynamically as time passes. By interpolating a date with styles like relative
, offset
, or timer
, we can present time-sensitive information that remains accurate without additional code.
For instance, to show the remaining time until a specific event, we can use the relative style. The Text
view will automatically calculate the difference between the current date and the target date, and update the displayed value in real time.
Text("\(showtime, style: .relative) left until showtime")


By default, digits in the text use proportional widths, meaning their spacing can vary depending on the numbers displayed. This might cause the text to shift slightly as it updates. To avoid this, we can apply the monospacedDigit()
modifier to ensure all numeric characters occupy the same width, while leaving other characters unaffected.
Text("\(showtime, style: .relative) left until showtime")
.monospacedDigit()
This approach ensures the text remains visually stable as the numeric values change, improving the overall appearance of the interface.
# Plurals
SwiftUI leverages the Foundation framework's automatic grammar agreement feature to simplify handling plurals and reduce the need for extensive localization strings. This ensures that text follows grammatical rules, such as pluralization, without requiring additional logic in our code.
To automatically adjust text for plural values, we can use the inflection rule and define the scope of the adjustment with the following syntax:
Text("^[\(ticketsSold) ticket](inflect: true) sold")
SwiftUI parses this syntax as a custom Markdown attribute and applies the appropriate pluralization using Foundation's grammar engine. For instance, if ticketsSold
is 1
, the text will display "1 ticket sold". When the value increases to 2
or more, it will adjust to "2 tickets sold", "3 tickets sold", etc., ensuring grammatical accuracy.


The grammar engine supports multiple languages, applying the correct pluralization rules for each. Initially introduced in iOS 15 with support for English and Spanish, it has since been expanded. As of iOS 18, it includes languages such as German, French, Italian, Portuguese, Hindi, and Korean, making it a versatile solution for multilingual apps.