|
My macOS app generates PDF files using fixed size CATextLayers. Each text layer has text of different lengths and need to be word wrapped when the length of the text line exceeds the bounds of the frame. However, if the lines of text exceed the height of the bounding frame, the font size will need to be reduced by one and then rechecked to ensure the text is contained within the bounding box. I'm assuming the wrapping would be automatic if the font size is reduced, i.e. re-wrapped? But, it seems everything I read is for UIKit and using UIFont, which doesn't work in macOS. It appears This is what I've been toying around with, but I'm getting a crash "Bad Execution" error. Also, I do not know how to set the nsString font size or word wrapping attributes. Am I approaching this totally wrong?
Appreciate any help or leading me into a direction where I can work out a solution. |
|
I figured this out. I found a way to check the boundingRect of a string based on the assigned attributes.
And then I create a textLater based on this class:
I can now center text both horizontally and vertically and resize it until it fits with a specified height and width. I am only getting the height from the method, but you can get all the coordinates by returning CGRect rather than an integer. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.