Scaling fonts to fit width, driving me crazy....

95 views
Skip to first unread message

Rikard Lindgren

unread,
Jan 16, 2014, 6:55:17 PM1/16/14
to codenameone...@googlegroups.com
We are banging our heads trying to figure the best way to scale fonts to fit a certain Width.

Example:
We want 20 characters to fit into a component, always, independent on device and resolution.

So even if its a Tablet with a very high res, we still want to be able to place just that amount of chars in a row.
Our system works flawless in the SIM, we simply measure the component and divide it by 20 and set that as font size. (As most fonts are higher than wide, and the ones we use are like that. Then we are sure that the numb of chars will fit)

But then when it comes to devices, we have PPI to consider as well...that's when things starts to get messy.

Is there anyone who have an idea on how to proceed?
On Android there is a way, as you can use XXdp where dp will scale depending on density. But i haven't found any info on that for iOS...

We simply need a solution on how to scale fonts to fit a certain amount of chars into a component, so it looks equal on all devices and OS´s.

Shai Almog

unread,
Jan 17, 2014, 2:25:08 AM1/17/14
to codenameone...@googlegroups.com
You have a conceptual problem both lines below have 20 characters:
iiiiiiiiiiiiiiiiiiii
WWWWWWWWWWWWWWWWWWWW

In Codename One we have Display.convertToPixels() which is the equivalent of Androids DIP's that works on all platforms. Notice that according to Google DIP's can have up to 20% margin of error.

Fuad Nassar

unread,
Jun 1, 2019, 8:58:46 PM6/1/19
to CodenameOne Discussions
i have same these problems 
is there any new solution ??

Shai Almog

unread,
Jun 1, 2019, 11:13:30 PM6/1/19
to CodenameOne Discussions
This is a post from 5 years ago. It's out of date https://www.codenameone.com/blog/autosizing-add-all-ios-redirects.html

Dave Dyer

unread,
Jun 3, 2019, 2:23:29 AM6/3/19
to CodenameOne Discussions
Devices claimed PPI is at best a rough guide to the size needed for legibility, but given a box size, FontMetrics.stringWidth() is accurate.
If the box doesn't fit, you can derive larger or smaller fonts and try again. It helps if you keep track of what font is being used so you
Know what constitutes a step smaller.
Reply all
Reply to author
Forward
0 new messages