Calculate ems value according to the string length count

314 Views Asked by At

I want to calculate the ems value according to the string or text length count If text length increase it should occupy it's width size according to the characters count without cut-off text in a single line, thanks

Tried to keep string length as ems but it gives very long width size with empty spaces, Thanks in advance

1

There are 1 best solutions below

0
On

Using monospaced fonts you could count the em-width of a string. Since letters like i and m do in other cases not have the same length, the pixel/em/dp count cannot be predicted.

If someone would write a sophisticated logic for the widths of letters, count them, then multiply count with per-letter-length, this solution would still be too inaccurate without even more refinements: Ligatures and kerning make the actual width of a word even more complex. For example, A and V can overlap a little, while a capital A following another A or the letter V following another V are same width at at base or top, respectively. Therefore their centers have to be moved away from each other so as to not overlap.