Forum Moderators: open
I have a field that I need users to fill in but the title cannot spill over onto two lines so I need to limit what they input. If I add a character count this doesn't necessarily work because some characters are bigger than others.i is smaller than w in most fonts. Therefore is there a way of doing this or do I just have to use a fixed width font such as courier?
I hope all that makes sense and thanks in advance for any help.
This is possible in a desktop application when the environment (and importantly the fonts) is known, but on a browser on the www where there is a lot of unknown then trying to be pixel perfect is a mighty tricky affair!
You could try and approximate it. Perhaps give each character a width based on a known font/size and calculate the overall width once the user has submitted the text (form validation). There may be a gfx library that will do this more accurately? But there is no guarantee that the avg web user will have the font you have based your approximation on.
Even using a fixed width font may not be the answer - what size?!
Alternatively, allow your users to enter any title they like (restricted to a sensible max number of chars). Then simply restrict the area in which the title appears (overflow:hidden) so that it won't spill onto two lines. If the title doesn't fit, give your users the option to edit it.
... welcome to WebmasterWorld! :)