As @kontur has already pointed out, there are many considerations when doing this. But if you are looking for a rough but generally workable solution I would consider something like the following.
Set the width based on a function of the number of characters in the string, and use a standard word wrap to determine the layout. As a rough example:
- Devise a simple algorithm array to determine your layout where the first element contains a tuple of two elements. The first representing the max number of characters to spread over one line, and the second containing, the width (in whatever units you are using) of the area. The second element would be a tuple containing the max number of characters to spread over three lines, and the width that you would want to use for that area. And continue on as long as you like. An example (using em's as my units) may be:
[(30, 15), (48, 14), (62, 13),...]
- Count the number of characters in the string.
- Check the elements of the algorithm array to find the first one for which the first element of the tuple is higher than the number of characters, and set the width equal to the value in the second element.
This will not look as good as an advanced layout algorithm, but will get the job done quickly. Some further examples to help clarify this (using the algorithm array given above):
Characters = 49, width = 14
Characters = 28, width = 12
Characters = 63, width = 13
You will need to play with the choice of numbers for the algorithm array to get it to what you want, but it should be better than a fixed max width, and easier to implement than any advanced algorithm.