Tag

public struct Tag : View

The generic tag control displays complementary information that relates to the current page

  • Creates a tag view that displays a string literal without localization.

    Use this initializer to create a tag view with a string literal without performing localization:

    Tag(verbatim: "pencil") // Displays the string "pencil" in any locale.
    

    If you want to localize a string literal before displaying it, use the init(_:tableName:bundle:comment:) initializer instead. If you want to display a string variable, use the init(_:) initializer, which also bypasses localization.

    Declaration

    Swift

    public init(verbatim content: String)

    Parameters

    content

    A string to display without localization.

  • Creates a tag view that displays a stored string without localization.

    Use this initializer to create a tag view that displays — without localization — the tag in a string variable.

    Tag(someString) // Displays the contents of `someString` without localization.
    

    SwiftUI doesn’t call the init(_:) method when you initialize a tag view with a string literal as the input. Instead, a string literal triggers the init(_:tableName:bundle:comment:) method — which treats the input as a LocalizedStringKey instance — and attempts to perform localization.

    By default, SwiftUI assumes that you don’t want to localize stored strings, but if you do, you can first create a localized string key from the value, and initialize the text view with that. Using a key as input triggers the init(_:tableName:bundle:comment:) method instead.

    Declaration

    Swift

    public init(_ attributedContent: AttributedString)

    Parameters

    content

    The attributed string value to display without localization.

  • Creates a tag view that displays a stored string without localization.

    Use this initializer to create a tag view that displays — without localization — the tag in a string variable.

    Tag(someString) // Displays the contents of `someString` without localization.
    

    SwiftUI doesn’t call the init(_:) method when you initialize a tag view with a string literal as the input. Instead, a string literal triggers the init(_:tableName:bundle:comment:) method — which treats the input as a LocalizedStringKey instance — and attempts to perform localization.

    By default, SwiftUI assumes that you don’t want to localize stored strings, but if you do, you can first create a localized string key from the value, and initialize the text view with that. Using a key as input triggers the init(_:tableName:bundle:comment:) method instead.

    Declaration

    Swift

    public init(_ content: some StringProtocol)

    Parameters

    content

    The string value to display without localization.

  • Declaration

    Swift

    public var body: some View { get }
  • Creates an instance that wraps an Image, suitable for concatenating with other Text

    Declaration

    Swift

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    init(_ image: Image)
  • Creates a Tag view that displays localized content identified by a key.

    Use this initializer to look for the key parameter in a localization table and display the associated string value in the initialized text view. If the initializer can’t find the key in the table, or if no table exists, the text view displays the string representation of the key instead.

    Tag("pencil") // Localizes the key if possible, or displays "pencil" if not.
    

    When you initialize a text view with a string literal, the view triggers this initializer because it assumes you want the string localized, even when you don’t explicitly specify a table, as in the above example. If you haven’t provided localization for a particular string, you still get reasonable behavior, because the initializer displays the key, which typically contains the unlocalized string.

    If you initialize a text view with a string variable rather than a string literal, the view triggers the Text/init(_:)-9d1g4 initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(_:tableName:bundle:comment:) initializer by first creating a LocalizedStringKey instance from the string variable:

    Text(LocalizedStringKey(someString)) // Localizes the contents of `someString`.
    

    If you have a string literal that you don’t want to localize, use the Text/init(verbatim:) initializer instead.

    Declaration

    Swift

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

    Parameters

    key

    The key for a string in the table identified by tableName.

    tableName

    The name of the string table to search. If nil, use the table in the Localizable.strings file.

    bundle

    The bundle containing the strings file. If nil, use the main bundle.

    comment

    Contextual information about this key-value pair.