Mark Bryan Ramirez
Mark Bryan Ramirez

Reputation: 261

How to draw text along circular path using SwiftUI

I need some help on how to generate a single line text in a circular path using SwiftUI. I am trying to replicate the curved text in these Image. Is this possible in SwiftUI? Thanks.

Upvotes: 10

Views: 7266

Answers (3)

Mark Bryan Ramirez
Mark Bryan Ramirez

Reputation: 261

enter image description hereFound a solution to my question. Credits to Tobias Due Munk for posting his prototype for CurvedText using SwiftUI. His solution can be found here https://github.com/duemunk/CrookedText

Example usage:

CrookedText(text: "Hello, world!", radius: 10)

Upvotes: 16

user3441734
user3441734

Reputation: 17534

  1. calculate position of each character, see How to get position of each character in SwiftUI Text and related answers and discussions, to know the limitations

  2. calculate required position and rotation of each character on the path you would like to follow (for circle it is very easy to be done) with help of step 1 (use the distance along the path as a parameter)

  3. position and rotate each character as here Positioning View using anchor point

I know that answers with links, as already mentioned by "Ru Chem Chong", is not the best, but answer your question is really complex task and explanation needs more than space available here.

Upvotes: 1

Tilak Madichetti
Tilak Madichetti

Reputation: 4346

No, Swift UI doesn't provide any way to do that as of now. So as @fulvio pointed out you must do it Swift UIKit way .

You can later use UIViewRepresentable to integrate it in your SwiftUI Project Refer Swift UI interfacing with UIKit by Apple Inc.

Upvotes: 1

Related Questions