跳转至
Structure

Font

A font.

Declaration

@frozen struct Font : Hashable

Overview

This structure defines a Font in SwiftUI. Font offers shorthands for varying styles of the system font via static properties, such as Font.body or Font.title. Font also includes many modifiers, including ways to compare & modify fonts.

Fonts can be applied to your view with the font(_:) modifier.

struct ExampleView: View {
    var body: some View {
        Text("Banana🍌🍌")
            .font(.largeTitle)
    }
}
A view displaying the text "Banana🍌🍌" in large title font.

In addition to standard system font types like largeTitle and body, you can customize the size, weight and design of your Font with the system(size:weight:design:) modifier.

struct ExampleView: View {
    var body: some View {
        Text("Banana🍌🍌")
            .font(.system(size: 32.0, weight: .bold, design: .rounded))
    }
}
A view displaying the text "Banana🍌🍌" in bolded, rounded, 32 point font.

Non system-fonts can be applied using custom(_:size:)

struct ExampleView: View {
    var body: some View {
        Text("Banana🍌🍌")
            .font(Font.custom("American Typewriter", size: 32.0))
    }
}
A view displaying the text "Banana🍌🍌" in the non-system font "American Typewriter."

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Property

body A font with the body text style.

callout A font with the callout text style.

caption A font with the caption text style.

caption2 Create a font with the alternate caption text style.

footnote A font with the footnote text style.

headline A font with the headline text style.

largeTitle A font with the large title text style.

subheadline A font with the subheadline text style.

title A font with the title text style.

title2 Create a font for second level hierarchical headings.

title3 Create a font for third level hierarchical headings.


Type Method

==(lhs:rhs:) Returns a Boolean value indicating whether two values are equal.

custom(_:fixedSize:) Create a custom font with the given name and a fixed size that does not scale with Dynamic Type.

custom(_:size:) Create a custom font with the given name and size that scales with the body text style.

custom(_:size:relativeTo:) Create a custom font with the given name and size that scales relative to the given textStyle.

system(_:design:) Gets a system font with the given style and design.

system(size:weight:design:) Specifies a system font to use, along with the style, weight, and any design parameters you want applied to the text.


Structure

Weight A weight to use for fonts.


Initializer

init(_:) Creates a custom font from a platform font instance.


Instance Method

bold() Adds bold styling to the font.

hash(into:) Hashes the essential components of this value by feeding them into the given hasher.

italic() Adds italics to the font.

leading(_:) Create a version of self that uses leading (line spacing) adjustment.

lowercaseSmallCaps() Adjusts the font to enable lowercase small capitals.

monospacedDigit() Adjusts the font to use monospace digits.

smallCaps() Adjusts the font to enable all small capitals.

uppercaseSmallCaps() Adjusts the font to enable uppercase small capitals.

weight(_:) Sets the weight of the font.


Enumeration

Design A design to use for fonts.

Leading The different types of line spacing.

TextStyle A dynamic text style to use for fonts.


Instance Property

hashValue The hash value.