- Home /
How to determine how much of string fits in UI.Text element?
Hey all! I was wondering if there was a way to tell if you could find out, given a string, where the line breaks in the string would be and how much of it would fit when it would be rendered on a UI.Text element? I need to know for an classic 2d-RPG like dialogue box that can't expand in size and that will include dialogue generated with variable length strings like variable character names. That means I can't manually pre-insert line breaks anywhere. Anyone have a clue on how to do this :) forever grateful, of course!
Hey. I'm not quite sure if this helps but you could try to play with Preferred Width and Preferred Height.
Answer by Bluestone19 · Jul 23, 2016 at 01:11 PM
I guess one (rather inefficient) method would be to have some sort of list of all the characters that will ever be used and their widths, including capitals/lowercase, symbols, spaces, etc. and then mathematically see how many lines it would take/where the lines break through that by comparing sums of character widths to the box width. I guess you could sort of remove this problem by making all characters the same width using a custom font (An NES font might be your best bet to find, but if you want something more natural-looking, I bet it wouldn't be hard to find another font, if not just make one from scratch).