FontSize4TextBox

version 1.0.0 (1.45 MB) by Peter Seibold
Find the largest possible font size that fits a text into a given static text box.

6 Downloads

Updated 19 Jun 2022

View License

Purpose
Find the largest possible font size that fits a text into a given static text box.
Features
  • Find the largest possible font size for a single line of text. File: FontSize4TextBoxSingleLine.m
  • Find the largest possible font size for multi lines of text. File: FontSize4TextBoxMultiLine.m
Method
For a single line find the largest font size by extent (very fast) or by image processing (exact).
For multiple lines find the largest font size by image processing.
By extent (very fast):
  1. Estimate roughly the font size by the box size and length of text.
  2. Find a small font size that fits the text inside the box. (lower limit)
  3. Find a large font size that exceeds the box size. (upper limit)
  4. Iterate between the two values until the larges possible font size is found.
By image processing:
  1. Create an invisible figure with a tall text box with the given width.
  2. Estimate font size by given box size and length of text.
  3. Set this font size in the invisible text box.
  4. Get the image content of the invisible text box by ‘getframe’
  5. Analyze the content: Font size too small, if position of last vertical text pixel smaller than given box height, Font size too large, if position of last vertical text pixel larger than given box height.
  6. Decrease or increase font size until a lower and an upper limit for the font size is found.
  7. Iterate between those limits.
  8. Do some post processing like overcome discontinuities, try some higher font sizes, etc.
Restrictions
  • Leading or trailing spaces discarded.
  • Static text box height limited to approx. 1/3 screen height.
  • Normalized units for font size or box size not supported.
  • Right alignment, especially with tall boxes, not well supported.
Usage
The code file ‘FontSize4TextBoxSingleLine.m’ is for a single line of text.
The code file ‘FontSize4TextBoxMultiLine.m’ is for multiple lines of text.
A third file, ‘DemoFontSize4TextBox.m’, is for demonstration and evaluation.
Inside the first two files you will find advices on how to use them. Also the demo-file displays the appropriate commands.
For more information, please read the enclosed pdf-file.

Cite As

Peter Seibold (2022). FontSize4TextBox (https://www.mathworks.com/matlabcentral/fileexchange/113585-fontsize4textbox), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with R2016a and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!