I'm working on a SwiftUI view where I need to concatenate two Text views and make only one part of the concatenated text tappable. Here is a simplified example.
Text("Hello, ") + Text("world!").bold()
I want to add a tap gesture such that only the "world!" part is tappable. However, I am aware that after concatenation with the + operator, the text becomes a single Text view, making it challenging to add a gesture recognizer to just a part of it.
Is there a way in SwiftUI to add a tap gesture recognizer to only a part of a concatenated Text view? Or is there an alternative approach to achieve a similar layout with part of the text being tappable?
My end goal is to create a toggle-able truncation such as this: Spotify demo
Another problem you are going to face in this particular case is finding a way to truncate the first text with ellipses and then append the "see more" without ellipses.
If you look closely at the demo, I don't think the ellipses are attached to the first text. So one way to implement is to include the ellipses with the button and appy it as an overlay in the bottom corner using alignment
.trailingLastTextBaseline. A gradient can be used to mask out the text that flows behind the button.To constrain the height of the long text, you can either set a fixed height, or just set a line limit. By setting a line limit, there is no excess vertical space and it adapts to different font sizes automatically.