Re: Adjusting Font Size to fit View Rectangle
This was for macOS… not sure how to go about it on iOS. I’d guess it would work, but there may be an easier method. I don’t know though.toggle quoted messageShow quoted text
I have used this method to dynamically size formatted multiline text (text that includes line breaks) to find the maximum font size for a fixed container size. Granted there may be an easier solution that I am not aware of, but I am also counting the number of line of text being laid out, so this solution works for me.
1. Get text
2. Setup your mutable paragraph style, set properties, justification, wrapping… etc.
3. Create a layout manager… set properties, hyphenation, etc. Important, set the layout mgr’s delegate (I used self for simplicity). You will receive callbacks about the layout process via delegation.
3a. Define delegate method -layoutManager:didCompleteLayoutForTextContainer:atEnd: (you will receive these callbacks where you can flag ‘clipping’ I.e. text too big) set a flag here indicating clipping has occurred. I used an iVar.
4. Create a mutable attributes dict, this contains the para style keyed on NSParagraphStyleAttributeName. (You will add/update the font later stored in this dict with different sizes later)
5. Create a text storage object with text and para style
6. Now loop… do while (notClipped && other things you care about)
a. Adjust font size
b. Create new NSFont object with new size. Set font in attributes dict keyed on NSFontAttributeName
c. Adjust paragraph style as needed/desired (optional)
d. Reapply (Set) the updated attributes dict for the entire range of text in your text storage object -setAtteibutes:range:
e. Ask the layout manager/text storage to layout the text. If the text is too big the container parameter in your callback will be nil indicating that ‘clipping’ occurred. Use nil container to flag clipping thus communicating with your loop.
Rinse, repeat… till desired font size reached
The text generated using this approach is displayed in widgets that are part of a complex visual graph of objects all of which are the same size and which contain dynamic multiline text. I have additional logic that counts the number of lines to ensure that there are always three lines. Each line contains a different property of the object being represented. IP, hostname, etc… Think graphical displays of networks and such, with text inside the network components.
On Sep 20, 2018, at 05:51, Dave <dave@...> wrote: