ComposableArchitecture Documentation

Structure Text​State

public struct TextState: Equatable, Hashable  

An equatable description of SwiftUI Text. Useful for storing rich text in state for the purpose of rendering in a view hierarchy.

Although SwiftUI.Text and SwiftUI.LocalizedStringKey are value types that conform to Equatable, their == do not return true when used with seemingly equal values. If we were to naively store these values in state, our tests may begin to fail.

TextState solves this problem by providing an interface similar to SwiftUI.Text that can be held in state and asserted against.

Let's say you wanted to hold some dynamic, styled text content in your app state. You could use TextState:

struct AppState: Equatable {
  var label: TextState
}

Your reducer can then assign a value to this state using an API similar to that of SwiftUI.Text.

state.label = TextState("Hello, ") + TextState(name).bold() + TextState("!")

And your view store can render it directly:

var body: some View {
  WithViewStore(self.store) { viewStore in
    viewStore.label
  }
}

Certain SwiftUI APIs, like alerts and confirmation dialogs, take Text values and, not views. To convert TextState to SwiftUI.Text for this purpose, you can use the Text initializer:

Alert(title: Text(viewStore.label))

The Composable Architecture comes with a few convenience APIs for alerts and dialogs that wrap TextState under the hood. See AlertState and ActionState accordingly.

In the future, should SwiftUI.Text and SwiftUI.LocalizedStringKey reliably conform to Equatable, TextState may be deprecated.

%35 TextState TextState Equatable Equatable TextState->Equatable Hashable Hashable TextState->Hashable View View TextState->View CustomDumpRepresentable CustomDumpRepresentable TextState->CustomDumpRepresentable

Conforms To

CustomDumpRepresentable
Equatable
Hashable
View

Initializers

init(verbatim:​)

public init(verbatim content: String)  

init(_:​)

@_disfavoredOverload
  public init<S>(_ content: S) where S: StringProtocol  

init(_:​table​Name:​bundle:​comment:​)

public init(
    _ key: LocalizedStringKey,
    tableName: String? = nil,
    bundle: Bundle? = nil,
    comment: StaticString? = nil
  )  

Properties

body

public var body: some View  

custom​Dump​Value

public var customDumpValue: Any  

Methods

baseline​Offset(_:​)

public func baselineOffset(_ baselineOffset: CGFloat) -> Self  

bold()

public func bold() -> Self  

font(_:​)

public func font(_ font: Font?) -> Self  

font​Weight(_:​)

public func fontWeight(_ weight: Font.Weight?) -> Self  

foreground​Color(_:​)

public func foregroundColor(_ color: Color?) -> Self  

italic()

public func italic() -> Self  

kerning(_:​)

public func kerning(_ kerning: CGFloat) -> Self  

strikethrough(_:​color:​)

public func strikethrough(_ active: Bool = true, color: Color? = nil) -> Self  

tracking(_:​)

public func tracking(_ tracking: CGFloat) -> Self  

underline(_:​color:​)

public func underline(_ active: Bool = true, color: Color? = nil) -> Self  

Operators

+

public static func + (lhs: Self, rhs: Self) -> Self