Font Metrics

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Leximatica
Posts: 4
Joined: Wed Apr 15, 2009 1:39 am

Font Metrics

Post by Leximatica » Wed Apr 15, 2009 1:46 pm

How do I obtain font metric information, such as the width of a formatted string, baseline, ascent, etc.

For example, how would I manually resize a button to accommodate a change to its label font and size?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Apr 15, 2009 2:42 pm

Dear Leximatica,

Code: Select all

set the height of btn 1 to (the formattedHeight of btn 1) + 16
set the width of btn 1 to (the formattedWidth of btn 1) + 16
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Font Metrics

Post by FourthWorld » Wed Apr 15, 2009 2:56 pm

Leximatica wrote:How do I obtain font metric information, such as the width of a formatted string, baseline, ascent, etc.

For example, how would I manually resize a button to accommodate a change to its label font and size?
To set the size of a button to fit its text:

Code: Select all

set the width of me to (the formattedWidth of me + 24)
set the height of me to (the effective textHeight of me + 12)
You may want to adjust those margins, but the formattedWidth and the textHeight are all you need.

The "effective" keyword is a useful habit for many text properties like textHeight, because it returns the value used to render the control rather than the actual setting in the control itself.

Text and color properties are inherited, and if you set those properties to empty they assume those of their owner. Using "the effective textHeight" will return the textHeight used to render the control, even if the control's own textHeight property is empty.

The formattedWidth and formattedHeight properties work for most objects, returning the value needed to contain a control's contents.

In a button, it returns the width of the button after accounting for its text rendered at whatever its effective textFont, textSize, textFont and contents are, plus the borders and margins of the button.

For groups and cards, the formattedWidth and formattedHeight return the bounding rect comprised of all objects contained in that group or card.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Leximatica
Posts: 4
Joined: Wed Apr 15, 2009 1:39 am

Post by Leximatica » Wed Apr 15, 2009 4:53 pm

That helps a great deal. Thanks.

Post Reply