Why this happens
SwiftUI’s Text view does not inherit the scaledToFit() or scaledToFill() modifiers that work on images and shapes. Those modifiers belong to the Image and Shape protocols, not to Text. When you apply .scaledToFill() to a Text view, SwiftUI silently ignores it because Text conforms to View but not to the content-mode protocols that drive those modifiers.
The layout system treats text as having an intrinsic size derived from its font, content, and the proposed size from its parent. Without explicit constraints, text stays at its natural font size. If the parent (like a Circle with a fixed stroke border) is larger or smaller than that natural size, the text simply centers itself without scaling.
This behavior is intentional: text readability and accessibility (Dynamic Type) are prioritized over filling arbitrary shapes. However, designs often require text that visually fills a badge, circle, or card. The solutions below give you control while still respecting accessibility when configured correctly.
How to fix it
1. GeometryReader — precise, layout-aware scaling
Use GeometryReader to read the parent’s proposed size and compute a font size that fits the smaller dimension. This approach adapts to rotation, size classes, and any container size changes.
struct ScaledTextInCircle: View {
let text: String
let strokeColor: Color
let lineWidth: CGFloat
let fontScale: CGFloat // fraction of the smaller side
var body: some View {
GeometryReader { proxy in
let size = min(proxy.size.width, proxy.size.height)
let fontSize = size * fontScale
ZStack {
Circle()
.strokeBorder(strokeColor, lineWidth: lineWidth)
Text(text)
.font(.system(size: fontSize, weight: .bold))
.minimumScaleFactor(0.5) // safety net for long strings
.lineLimit(1)
}
.frame(width: proxy.size.width, height: proxy.size.height)
}
}
}
// Usage
ScaledTextInCircle(
text: "42",
strokeColor: .red,
lineWidth: 30,
fontScale: 0.4
)
.frame(width: 200, height: 200)
Key points
– min(proxy.size.width, proxy.size.height) ensures the text fits both portrait and landscape.
– fontScale (e.g., 0.4) lets you tune the visual weight; adjust per design.
– .minimumScaleFactor(0.5) prevents clipping if the string is longer than expected.
– Wrap in a fixed-size frame or let the parent propose the size.
2. Large font + minimumScaleFactor — quick, no GeometryReader
If you want a one-liner without reading geometry, give the text an intentionally huge font and allow it to shrink aggressively. The layout system will shrink the text until it fits the proposed size.
ZStack {
Circle()
.strokeBorder(Color.red, lineWidth: 30)
Text("Text")
.font(.system(size: 500, weight: .bold))
.minimumScaleFactor(0.01)
.lineLimit(1)
.padding(40) // optional inner padding
}
.frame(width: 200, height: 200)
Why this works
– The proposed size from the frame (or parent) constrains the ZStack.
– Text starts at 500 pt, then the layout engine scales it down until it fits.
– minimumScaleFactor(0.01) allows up to 99% shrinkage.
– lineLimit(1) prevents wrapping, which would defeat the scaling.
Trade-offs
– Less precise: the final font size depends on the string length and character shapes.
– Accessibility: very small scale factors can produce unreadable text for long strings at small container sizes. Combine with .minimumScaleFactor(0.5) and test with Dynamic Type.
– No access to the exact container size for other calculations.
3. ViewThatFits (iOS 16+) — declarative fallback chain
For a more SwiftUI-native approach, provide multiple pre-sized text variants and let ViewThatFits pick the first one that fits.
ViewThatFits(in: .horizontal) {
Text("Text").font(.system(size: 200, weight: .bold))
Text("Text").font(.system(size: 150, weight: .bold))
Text("Text").font(.system(size: 100, weight: .bold))
Text("Text").font(.system(size: 50, weight: .bold))
.minimumScaleFactor(0.5)
}
.frame(width: 200, height: 200)
This avoids GeometryReader and works well when you have a discrete set of design-approved sizes.
How to prevent it
- Don’t reach for
scaledToFit()on Text — it does nothing. Remember it’s anImage/Shapemodifier. - Design with constraints first — decide whether the text should fill width, height, or the smaller dimension, then pick the matching technique.
- Test with Dynamic Type — enable larger accessibility text sizes in Settings → Accessibility → Display & Text Size. Verify your scaling doesn’t clip or become unreadable.
- Set
lineLimit(1)when you want single-line scaling; otherwise the layout may wrap instead of shrink. - Prefer
GeometryReaderfor reusable components — it gives you the exact size for other calculations (e.g., stroke width proportional to radius).
FAQ
Does minimumScaleFactor work with Dynamic Type?
Yes. The scale factor applies after the system applies the user’s preferred text size. If the user chooses a very large Dynamic Type size, the text may already exceed the container before scaling kicks in. Test with the largest accessibility sizes to ensure your minimumScaleFactor floor is reasonable.
Can I animate the font size change when the container resizes?
With GeometryReader, the font size updates instantly as the proxy size changes. Wrap the GeometryReader in a withAnimation block or apply .animation(.easeInOut, value: proxy.size) to the Text if you want smooth transitions during rotation or size-class changes.
Why does my text still clip even with minimumScaleFactor(0.01)?
Clipping usually comes from a parent view that constrains the layout before the text gets a chance to shrink. Ensure the immediate parent (often a ZStack, VStack, or frame) proposes the size you expect. Adding .frame(maxWidth: .infinity, maxHeight: .infinity) to the ZStack or using a fixed .frame(width:height:) on the outer container often resolves it.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.