Skip to content

Commit 90289b5

Browse files
committed
SwiftUI Text Gotcha
1 parent a57d3bc commit 90289b5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: 'SwiftUI Text `fixedSize` Gotcha'
3+
author: 'Clive Liu'
4+
layout: post
5+
tags: [Swift, SwiftUI]
6+
---
7+
8+
The `fixedSize` modifier in SwiftUI forces a view to adopt its ideal size, preventing it from being compressed or truncated.
9+
10+
For example, you can use it to make sure a long text isn’t cut off:
11+
12+
```swift
13+
Text(aVeryLongString)
14+
.fixedSize(horizontal: false, vertical: true)
15+
```
16+
17+
However, there’s a subtle gotcha: the `lineLimit` modifier takes precedence over `fixedSize`. If a `lineLimit` is applied anywhere in the view hierarchy and affects the text view, it will still truncate the text — even if `fixedSize` is used.
18+
19+
To avoid this, make sure no unintended `lineLimit` is affecting your text. A simple fix is to explicitly set `.lineLimit(nil)` on the `Text` view alongside `fixedSize`:
20+
21+
```swift
22+
Text(aVeryLongString)
23+
.lineLimit(nil)
24+
.fixedSize(horizontal: false, vertical: true)
25+
```
26+
27+
This ensures the text can grow vertically to fit its content as expected.

0 commit comments

Comments
 (0)