skip to content

In SwiftUI, how do you put a bold orange "Pending" badge inside one line of Text, and why does Xcode warn about Text + Text?

level: middleimportance: should knowfreq 50%

answer

  1. Text inside a Text literal
  2. modifiers that return Text
  3. deprecated in iOS 26
  4. a View in an interpolation

basics

~20 s

Interpolate a styled Text into a Text literal: Text("(merchant) (badge)"), where badge is Text("Pending").bold().foregroundStyle(.orange). The + operator on Text is deprecated in iOS 26 in favour of this interpolation, which keeps the sentence as one localization key.

solid answer

~40 s

Build the badge with modifiers that return `Text` (`bold()`, `fontWeight(_:)`, `font(_:)`, `foregroundStyle(_:)` from iOS 17) and interpolate it: `Text("\(merchant) \(badge)")`. `LocalizedStringKey`'s interpolation has accepted a `Text` since iOS 14, so the result is one `Text` that wraps and truncates as a single paragraph and produces one localization key. `Text + Text` did the same job, but the iOS 26 SDK deprecates `+` with the message "Use string interpolation on `Text` instead"; the warning appears once the deployment target is iOS 26 or later. Once a view modifier such as `padding(_:)` or `background(_:)` makes the badge a `View`, the interpolation falls back to an unlocalized debug description, and the compiler warns.

code

swift · 20 lines
swift
import SwiftUI

struct PendingLine: View {
    let merchant: String
    let isPending: Bool

    var body: some View {
        if isPending {
            let badge = Text("Pending")
                .bold()
                .foregroundStyle(.orange)   // Text-returning modifiers: still a Text
            // One Text, one localization key ("%@ %@"), wraps as one paragraph.
            Text("\(merchant) \(badge)")
                .lineLimit(2)
        } else {
            Text(merchant)
                .lineLimit(2)
        }
    }
}

go deeper

for a junior

Recall that a Text can be interpolated into a Text literal and that bold() and foregroundStyle() keep it a Text, so one line can mix styles.

for a middle

Explain why one interpolated Text wraps and localizes as one sentence, why + is deprecated in iOS 26, and why a padded badge becomes a debug description.

for a senior

Read the compiler warnings in review: the + deprecation tied to the deployment target and the debug-description interpolation, and decide whether the badge belongs in the paragraph or beside it.

for a principal

Plan the iOS 26 deployment-target bump with the + migration in it, and set a rule that styled sentences are built by interpolation so translators see whole sentences.

## The task A banking app's transaction row reads "Corner Café **Pending**" with the badge bold and orange, inside **one** run of text that wraps like a sentence. Two `Text` views in an `HStack` look right on a short line but break the moment the merchant name wraps: the badge sits beside the paragraph instead of flowing after its last word, and the two views no longer share one localization string. ## One Text, styled runs SwiftUI's way is to put a `Text` **inside** a `Text`: 1. Build the styled part with modifiers that **return `Text`**: `bold()`, `italic()`, `fontWeight(_:)`, `font(_:)`, `foregroundStyle(_:)` (the `Text`-returning overload is iOS 17), `underline(_:color:)`, `strikethrough(_:color:)`, `monospacedDigit()`, `baselineOffset(_:)`. 2. Interpolate it into a literal: `Text("\(merchant) \(badge)")`. `LocalizedStringKey.StringInterpolation` has had an `appendInterpolation(_:)` for `Text` since iOS 14. 3. Apply line modifiers such as `lineLimit(_:)` to the outer `Text`; they govern the whole paragraph. Apple's `Text` documentation recommends exactly this: interpolate `Text` (or `AttributedString`) to style parts of a string "while ensuring that the content can still be localized correctly". The key is `"%@ %@"`, so a translator can move the badge before the merchant in a language that needs it, which concatenation can never offer. ## Why + now warns `static func + (Text, Text) -> Text` has existed since iOS 13 and still works. In the iOS 26 SDK it carries: | Platform | Introduced | Deprecated | Message | |---|---|---|---| | iOS | 13.0 | 26.0 | Use string interpolation on `Text` instead | Deprecation warnings follow the **deployment target**: an app that still deploys to iOS 17 compiles `+` silently, and the warning appears once the minimum is iOS 26 or later. The reasons interpolation is preferred: - `+` fixes the word order in code, so a translation cannot reorder the parts. - Each operand is a separate string; `Text("Paid ") + Text(amount)` becomes two keys that translate out of context. - Interpolation reads as the sentence it renders. The migration is mechanical: `Text(a) + Text(b).bold()` becomes `Text("\(a)\(badge)")` with `let badge = Text(b).bold()`, keeping any spaces inside the literal. Because interpolating a `Text` has worked since iOS 14, the rewrite needs no availability check on an app that still supports iOS 17, so there is no reason to wait for the deployment-target bump that will surface the warnings. ## The View trap `badge.padding(4).background(.orange)` looks like a nicer badge, but `padding(_:)` and `background(_:)` are **view** modifiers: they return `some View`, not `Text`. Interpolating that value does not hit the `Text` overload; it falls to a disfavoured generic overload that is deprecated with the message "Localized string interpolation produces an unlocalized, debug description for this type of value", and the screen shows that debug description instead of the badge. The rule: - Text-returning modifiers keep the value a `Text`, so it can be interpolated. - Anything that adds a frame, padding, background or shape makes a view, which belongs outside the paragraph. - A filled background on part of a run is possible with `AttributedString`'s `backgroundColor` attribute, which is the AttributedString lesson's territory. ## Choosing the right tool for a mixed-style line | Need | Tool | Wraps as one paragraph | One localization key | |---|---|---|---| | Bold or coloured word in a sentence | interpolated `Text` | yes | yes | | Styling decided at runtime, or a filled run | `AttributedString` | yes | when interpolated into a literal | | Padded capsule, icon button, tappable badge | separate views in a stack | no | no | | Legacy code on any target | `Text + Text` | yes | no, one key per operand | The table is the interview answer in miniature: the closer a badge is to "a word with a style", the more it belongs inside the `Text`; the closer it is to "a control with a shape", the more it belongs outside it. ## Other things you can interpolate The same interpolation accepts an `Image` (iOS 14), so an SF Symbol can sit inline at the text's size, plus an `AttributedString` (iOS 15) and a value with a format style. Which symbol and how it renders belongs to the lesson on images and symbols.

  • The badge needs a rounded orange capsule behind it. Can it still live inside the Text?
    Not as a capsule: `background(_:)` and `clipShape(_:)` return a view, which cannot be interpolated into a `Text`. Either accept a filled run through `AttributedString`'s `backgroundColor` attribute, which has no padding or corner radius, or lay the badge out as a separate view and accept that it will not flow after a wrapped line.
  • Your app deploys to iOS 17. Should you still replace + now?
    Yes when you touch the code: interpolation has been available since iOS 14, so it costs nothing on the old target and fixes the localization problem `+` always had. The warning will appear for every `+` the day the deployment target reaches iOS 26, and changing them then is a larger diff.

  • Two Text views in an HStack wrap like one sentence
  • Text + Text is removed in iOS 26 and fails to compile
  • Any view can be interpolated into a Text literal and will render
  • padding() and background() keep a Text a Text
  • Concatenated Text produces one localization key for the whole line

Assumes the iOS 27 SDK; Text + Text is deprecated in iOS 26, and interpolating Text into a Text literal works from iOS 14.