I would like to add text, whether it be a UILabel or CATextLayer to a CGPath. I realize that the math behind this feature is fairly complicated but wondering if Apple provides this feature out of the box or if there is an open-source SDK out there that makes this possible in Swift. Thanks!
Is it possible to add a UILabel or CATextLayer to a CGPath in Swift, similar to Photoshop's type to path feature?
653 Views Asked by John Martz At
1
There are 1 best solutions below
Related Questions in SWIFT
- Navigate after logged in with webservice
- URLSession requesting JSON array from server not working
- When using onDrag in SwiftUI on Mac how can I detect when the dragged object has been released anywhere?
- Protect OpenAI key using Firebase function
- How to correct error: "Cannot convert value of type 'MyType.Type' to expected argument type 'Binding<MyType>'"?
- How to share metadata of an audio url file to a WhatsApp conversation with friends
- Using @Bindable with a Observable type in SwiftUI
- How to make a scroll view of 9 images in a forEach loop open on image 6 if image 6 is clicked on from a grid?
- Using MTLPixelFormat.rgba16Float results in random round-off errors
- Search and highlight text of current text in PDFKit Swift
- How is passing a function as a parameter related to escaping autoclosure?
- Actionable notification api call not working in background
- Custom layout occupies all horizontal space
- Is it possible to fix slow CKAsset loading on Cloudkit?
- Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value - MapView.isMyLocationEnabled
Related Questions in UILABEL
- How to UILabel Linebreak set character wrap + truncate tail?
- How I make "...more" feature like instagram by using UILabel
- Arabic Diacritics (Harakat) Misaligned in UILabel with Justified Text Alignment
- HTML tags in UILabel don't work and instead got shown
- UILabel with large font size causing huge memory usage
- UILabel not drawing text properly in UICollectionViewCell
- Dynamic Resizing of UILabel Based on Text Content
- Content hugging and content compression resistance with minimum margin between views
- UITextView causes abnormal number of gesture recognizer dependencies
- how to have "hour", "minute", "second" text behind UIPickView
- Why UILabel giving extra padding in swift?
- Allow label to use preferred width necessary to not character wrap (when word wrap is enabled)
- How can I get the x, y, width and height parameters of the text inside the UILabel?
- How to rotate the contents in a label?
- Swift UI center text below label and icon
Related Questions in CALAYER
- App crashes of type EXC_BREAKPOINT (SIGTRAP) SIGNAL 5 Trace/BPT trap on iOS17
- Inserting subview into view where I've added subviews and sublayers
- Performance slowdown and inaccuracies in stroke rendering due to float-based distance calculation in drawing
- When using UIScrollView, how can I fade the left and right edges if content is outside the visible bounds?
- How to render content of CALayer and sync with it in real-time
- Issue with Sequential Touch Points in Using CALayer for Drawing in Swift
- layerClass breaks auto layout
- why does write addSublayer twice to block screenshot in swift?
- CALayer in SwiftUI: add CALayer circleLayer to view Camera layer but not show it on the screen
- How to make CALayer adapt to the application appearance using drawlayer:inContext:?
- Synchronize CARenderer with .private MTLTextuer
- Synchronize UIView CALayer draw with UIScrollView
- Flickering while rendering a CALayer -> MTLTexture, via CARenderer on background thread
- CALayer disappears when storing in a property
- Overlapping multiple CAShapelayers of the same size
Related Questions in CGPATH
- Xcode analyzer is showing leak for CGPathRef when it's in a class instance method?
- How to Achieve a Calligraphy Effect in SwiftUI Starting with a Single Curved Path
- How to make arrow on bubble reach all the way to enclosing rectangle
- How can I draw rectangular paths at specified times in swift using CAKeyframeAnimation?
- How to make a Line follow a CGPoint in SwiftUI?
- Applying mask to UIImageView results in wrong mask placement
- I'm getting a CoreGraphics API Error while making an interactive chart with SwiftUI Charts Framework
- Reverse mask with multiple overlapping rectangles
- How to animate this shape animation with rounded corners?
- Stroking CGPath in swift
- CGPath intersection(_:using:) and friends (new in iOS 16) broken?
- Viewing complex objects in debugger
- Swift Best Way to Save CGMutablePath as JSON
- Swift Combine and reverse UIBezierPaths
- Is there a way to set border to round rectangle that is added to CGMutablePath in Swift?
Related Questions in CATEXTLAYER
- SwiftUI How can I get the correct font size for CATextLayer
- CATextLayer is blurry on non-retina display
- How to measure character bounds within a text shown by CATextLayer?
- Using a CATextLayer to Mask Out From Another CALayer
- Different font output for same NSAttributedString with CATextLayer and NSTextView
- Animate `CATextLayer` font-size from bottom left
- how to add test on a vedio or CAtextlayer. i want to show text on the recorded videos. and when i do it audo is fine video go blank.Thanks in Advance
- CATextLayer's text cut off
- CATextLayer position and Transformation issue
- Insert CATextLayer inside UIBezierPath and rotate
- CATextLayer on video pixelated text
- Swift do not repaint CATextLayer
- Issue while assigning UITextView frame to CATextLayer
- CATextLayer cannot align to center horizontally with UILabel text in swift
- How to reduce the quality of the text inside UITextView with Swift
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?

You'll need to do this by hand, by computing the Bezier function and its slope at each point you care about, and then drawing a glyph at that point and rotation. You'll need to know 4 points (traditionally called P0-P3). P0 is the starting point of the curve. P1 and P2 are the control points. And P3 is the ending point in the curve.
The Bezier function is defined such that as the "t" parameter moves from 0 to 1, the output will trace the desired curve. It's important to know here that "t" is not linear. t=0.25 does not necessarily mean "1/4 of the way along the curve." (In fact, that's almost never true.) This means that measuring distances long the curve is a little tricky. But we'll cover that.
First, you'll need the core functions and a helpful extension on CGPoint:
t*t*tis dramatically faster than using thepowfunction, which is why the code is written this way. These functions will be called a lot, so they need to be reasonably fast.Then there is the view itself:
First it includes the control points, and the text:
Every time the text is changed, the layoutManager recomputes the locations of all of the glyphs. We'll later adjust those values to fit the curve, but these are the baseline. The positions are the positions of each glyph relative to the fragment origin, which is why we need to keep track of that, too.
Some odds and ends:
The Bezier function is actually a one-dimensional function. In order to use it in two dimensions, we call it twice, once for x and once for y, and similarly to compute the rotations at each point.
One last piece of housekeeping, and it'll be time to dive into the real function. We need a way to compute how much we must change "t" (the offset) in order to move a certain distance along the path. I do not believe there is any simple way to compute this, so instead we iterate to approximate it.
OK, finally! Time to draw something.
And with that you can draw text along any cubic Bezier.
This doesn't handle arbitrary CGPaths. It's explicitly for cubic Bezier. It's pretty straightforward to adjust this to work along any of the other types of paths (quad curves, arcs, lines, and even rounded rects). However, dealing with multi-element paths opens up a lot more complexity.
For a complete example using SwiftUI, see CurvyText.