Structure

Text

A view that displays one or more lines of read-only text.

Declaration

struct Text

Topics

Creating a Text View

init<S>(S)

Creates a text view that displays the specified content.

init(LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?)

Creates a text view that displays localized content identified by a key.

init(verbatim: String)

Creates a text view that displays the specified string.

Styling a Text View

func bold() -> Text

Applies a bold font weight to the text.

func italic() -> Text

Applies italics to the text.

func color(Color?) -> Text

Sets the color of the text.

func fontWeight(Font.Weight?) -> Text

Sets the font weight of the text.

func baselineOffset(Length) -> Text

Sets the baseline offset for the text.

func kerning(Length) -> Text

Sets the tracking for the text.

func underline(Bool, color: Color?) -> Text

Applies an underline to the text.

func strikethrough(Bool, color: Color?) -> Text

Applies a strikethrough to the text.

Specifying the Relative Size of a View

func relativeSize(width: Length, height: Length) -> _ModifiedContent<Text, _RelativeLayoutTraitsLayout>

Sets the size of the view to the specified proportion of its parent’s width and height.

func relativeWidth(Length) -> _ModifiedContent<Text, _RelativeLayoutTraitsLayout>

Sets the width of the view to the specified proportion of its parent’s width.

func relativeHeight(Length) -> _ModifiedContent<Text, _RelativeLayoutTraitsLayout>

Sets the height of the view to the specified proportion of its parent’s height.

func layoutPriority(Double) -> _ModifiedContent<Text, _TraitWritingModifier<Double>>

Sets the priority by which a parent layout should apportion space to the child.

Setting the Position of a View

func position(CGPoint) -> _ModifiedContent<Text, _PositionLayout>

Fixes the center of the view at the specified point in its parent’s coordinate space.

func position(x: Length, y: Length) -> _ModifiedContent<Text, _PositionLayout>

Fixes the center of the view at the specified coordinates in its parent’s coordinate space.

func offset(CGSize) -> _ModifiedContent<Text, _OffsetEffect>

Offsets the view by the horizontal and vertical distances in the given size.

func offset(x: Length, y: Length) -> _ModifiedContent<Text, _OffsetEffect>

Offsets the view by the specified horizontal and vertical distances.

func edgesIgnoringSafeArea(Edge.Set) -> _ModifiedContent<Text, _SafeAreaIgnoringLayout>

Extends the view out of the safe area on the specified edges.

Adjusting the Padding of a View

func padding(Length) -> _ModifiedContent<Text, _PaddingLayout>

Pads the view along all edge insets by the specified amount.

func padding(EdgeInsets) -> _ModifiedContent<Text, _PaddingLayout>

Pads the view using the specified edge insets.

Adjusting Text in a View

func font(Font?) -> Text

Sets the default font for text in the view.

func lineLimit(Int?) -> _ModifiedContent<Text, _EnvironmentKeyWritingModifier<Int?>>

Sets the maximum number of lines that text can occupy in the view.

func minimumScaleFactor(Length) -> _ModifiedContent<Text, _EnvironmentKeyWritingModifier<Length>>

Sets the minimum amount that text scales down to fit the available space.

func truncationMode(Text.TruncationMode) -> _ModifiedContent<Text, _EnvironmentKeyWritingModifier<Text.TruncationMode>>

Sets the truncation mode for lines of text that are too long to fit in the available space.

enum Text.TruncationMode

The kinds of truncation to perform when a line of text is too long to fit into the available space.

func allowsTightening(Bool) -> _ModifiedContent<Text, _EnvironmentKeyWritingModifier<Bool>>

Sets whether text can compress the space between characters when necessary to fit text in a line.

func flipsForRightToLeftLayoutDirection(Bool) -> _ModifiedContent<Text, _FlipForRTLEffect>

Sets whether the view flips its contents horizontally when the layout direction is right-to-left.

Adding Animations to a View

func animation(Animation?) -> _ModifiedContent<Text, _TransactionModifier>

Applies the given animation to all animatable values within the view.

func animation<V>(Animation?, value: V) -> _ModifiedContent<Text, _AnimationModifier<V>>

Applies the given animation to the view when the specified value changes.

Handling View Taps and Gestures

func tapAction(count: Int, () -> Void) -> _AutoResultView<Text>

Sets up actions that trigger in response to a tap gesture.

func gesture<T>(T, including: GestureMask) -> _AutoResultView<Text>

Attaches a gesture to the view with a lower precedence than gestures defined by the view.

func highPriorityGesture<T>(T, including: GestureMask) -> _AutoResultView<Text>

Attaches a gesture to the view with a higher precedence than gestures defined by the view.

func simultaneousGesture<T>(T, including: GestureMask) -> _AutoResultView<Text>

Attaches a gesture to the view to process simultaneously with gestures defined by the view.

func digitalCrownRotation<V>(Binding<V>) -> _ModifiedContent<Text, _DigitalCrownModifier>

Tracks Digital Crown rotations by updating the specified binding.

func itemProvider(Optional<() -> NSItemProvider?>) -> _ModifiedContent<Text, _TraitWritingModifier<Optional<() -> NSItemProvider?>>>

Provides a closure that vends the drag representation to be used for a particular data element.

func transaction((inout Transaction) -> Void) -> _ModifiedContent<Text, _TransactionModifier>

Applies the given transaction mutation function to all transactions used within the view.

Handling View Events

func onReceive<P>(P, perform: (P.Output) -> Void) -> SubscriptionView<P, Text>

Adds an action to perform when the specified publisher emits an event.

func onReceive<P>(P, perform: () -> Void) -> SubscriptionView<P, Text>

Adds an action to perform when the specified publisher emits an event.

func onCommand(Command, perform: (() -> Void)?) -> _AutoViewWrapper

Sets up an action that triggers in response to the given command.

func onExitCommand(perform: (() -> Void)?) -> _AutoResultView<Text>

Sets up an action that triggers in response to receiving the exit command while the view has focus.

func onMoveCommand(perform: ((Command.MoveDirection) -> Void)?) -> _AutoResultView<Text>

Sets up an action that triggers in response to receiving the move command while the view has focus.

func onPlayPauseCommand(perform: (() -> Void)?) -> _AutoResultView<Text>

Sets up an action that triggers in response to receiving the play or pause command while the view has focus.

func onPaste(of: [String], perform: ([NSItemProvider]) -> Void) -> _AutoViewWrapper

Sets up an action that triggers in response to the system paste command.

func onPaste<Payload>(of: [String], validator: ([NSItemProvider]) -> Payload?, perform: (Payload) -> Void) -> _AutoViewWrapper

Sets up an action that triggers in response to the system paste command that you validate with the given closure.

Handling View Hover and Focus

func onHover(perform: (Bool) -> Void) -> _ModifiedContent<Text, _HoverRegionModifier>

Adds an action to perform when the user moves the pointer over or away from the view’s frame.

func focusable(Bool, onFocusChange: (Bool) -> Void) -> _ModifiedContent<Text, _FocusableModifier>

Specifies if the view is focusable and, if so, adds an action to perform when the view comes into focus.

Supporting Drag and Drop in Views

func onDrag(() -> NSItemProvider) -> _ModifiedContent<Text, _DraggingModifier>

Activates the view as the source of a drag and drop operation.

func onDrop(of: [String], delegate: DropDelegate) -> _ModifiedContent<Text, _DropModifier>

Defines the destination of a drag and drop operation with the same size and position as the view using behavior controlled by the given delegate.

func onDrop(of: [String], isTargeted: Binding<Bool>?, perform: ([NSItemProvider], CGPoint) -> Bool) -> _ModifiedContent<Text, _DropModifier>

Defines the destination of a drag and drop operation with the same size and position as the view by handling dropped content and the drop location with the specified closure.

func onDrop(of: [String], isTargeted: Binding<Bool>?, perform: ([NSItemProvider]) -> Bool) -> _ModifiedContent<Text, _DropModifier>

Defines the destination for a drag and drop operation with the same size and position as the view by handling dropped content with the specified closure.

Applying Graphical Effects to a View

Styling Control Views

func pickerStyle<S>(StaticMember<S>) -> _ModifiedContent<Text, _PickerStyleWriter<S>>

Sets the style for pickers within the view’s environment.

func datePickerStyle<S>(StaticMember<S>) -> _ModifiedContent<Text, _DatePickerStyleModifier<S>>

Sets the style for date pickers within the view’s environment.

func textFieldStyle<S>(StaticMember<S>) -> _ModifiedContent<Text, TextFieldStyleModifier<S>>

Sets the style for text fields within the view’s environment.

func toggleStyle<S>(StaticMember<S>) -> _ModifiedContent<Text, _ToggleStyleModifier<S>>

Sets the style for toggles within the view’s environment.

func horizontalRadioGroupLayout() -> _AutoResultView<Text>

Sets the style for radio group style pickers within the view’s environment to be radio buttons horizontally arranged inside of the layout.

Configuring a List View

func listStyle<S>(StaticMember<S>) -> _AutoResultView<(Text, S)>

Sets the style for lists within the view’s environment.

func listRowPlatterColor(Color?) -> _ModifiedContent<Text, _TraitWritingModifier<Color?>>

Sets the color to apply to the system cell platter of the view when placed in a list.

func tag<V>(V) -> _ModifiedContent<Text, _TraitWritingModifier<_ViewTag<V>>>

Sets a tag for the view in order to differentiate it from a list of view options.

Configuring Navigation and Status Bar Views

func navigationBarTitle(Text) -> _AutoResultView<Text>

Configures the title in the navigation bar for the view.

func navigationBarHidden(Bool) -> _AutoResultView<Text>

Hides the navigation bar for the view.

func statusBar(hidden: Bool) -> _AutoResultView<Text>

Sets the visibility of the status bar for the view.

Configuring Navigation and Tab Bar Item Views

func navigationBarItems<L>(leading: L) -> _AutoResultView<Text>

Configures the navigation bar items for the view.

func navigationBarItems<T>(trailing: T) -> _AutoResultView<Text>

Configures the navigation bar items for the view.

Configuring Touch Bar Views

func touchBar<Content>(TouchBar<Content>) -> _ModifiedContent<Text, _TouchBarModifier<Content>>

Sets the touch bar and its content to be shown in the Touch Bar when applicable.

func touchBarItemPrincipal(Bool) -> _ModifiedContent<Text, _TraitWritingModifier<Bool>>

Sets the view as having special significance to the TouchBar.

func touchBarCustomizationLabel(Text) -> _ModifiedContent<Text, _TraitWritingModifier<Text>>

Sets a user-visible string identifying the view’s functionality.

Hiding and Disabling Views

func disabled(Bool) -> _ModifiedContent<Text, _EnvironmentKeyTransformModifier<Bool>>

Adds a condition for whether users can interact with the view.

func deleteDisabled(Bool) -> _ModifiedContent<Text, _TraitWritingModifier<Bool>>

Adds a condition for whether the view's view hierarchy is movable.

func moveDisabled(Bool) -> _ModifiedContent<Text, _TraitWritingModifier<Bool>>

Adds a condition for whether the view hierarchy for self can be moved.

Responding to View Preferences

Inspecting Views

func id<ID>(ID) -> IDView<Text, ID>

Generates a uniquely identified view that can be inserted or removed.

func equatable() -> EquatableView<Text>

Prevents the view from updating its child view when its new value is the same as its old value.

Implementing View Modifiers

typealias Text.Modified

The type resulting from applying a view modifier.

Relationships

Conforms To

See Also

Text

struct TextField

A control that displays an editable text interface.

Beta
struct SecureField

A control into which the user securely enters private text.

Beta
struct Font

An environment-dependent font.

Beta

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software