Member-only story

SwiftUI: How to make see more see less style button at the end of Text?

Prafulla Singh
2 min readFeb 27, 2021

--

Xcode 12 — iOS 14

We are going to divide this problem into 2 Parts.

  1. Detecting if text can/cannot fit in given number of line. On Button click reset line count for text.
  2. Evaluating text size to add read more/read less at the end to text in the last line and handle click event shrink and expand.

Detecting if text can/cannot fit in given number of line:

To evaluate if the text string going to truncate or not, We will have to render the text string two times. One with line limit another without line limit.

We are going to leverage main text view’s background view for the same. Also, we are going to set a button to expand shrink text with animation.

In this example we leverage main text view’s background to evaluate weather text is being shrunk or not. We put background as Zstack with maximum possible height and set the text without any line limit. Then compere the height to…

--

--

Prafulla Singh
Prafulla Singh

Responses (2)