Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit/AppGameKit Studio Showcase / [CODE] SetTextSize - unknown string but known width

Author
Message
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 9th Aug 2015 20:34 Edited at: 9th Aug 2015 20:47
I recently found the need to scale some text to fit a certain area. However, because I am using multiple languages I have no idea how large the text string will be.
I could use SetTextMaxWidth() but the downside to that is that any text that is too large will split onto the line below.
Instead I could use SetTextScissor() but that has the problem of cropping any text that doesn't fit.

Neither solution is desirable under every circumstance. It may be a better solution to set the size of the text to fit but if you don't know how big the text is then what size do you make it?

So, here is the answer: A function that will scale the text to fit a desired size.



And here it is in a full project that you can copy/paste and run:


Parameters
txtObj - A previously created text object
width - The screen width to fit the text into
AdjustIfFits - A boolean. If TRUE(1) then the text will be scaled up to adjust it to the 'width' parameter. If False(0) then the text will not be scaled if it already fits within the required width but is smaller.


Because the function contains a loop I wouldn't recommend using it in real-time but it is perfect for setting up a HUD before a game.

Here is an example from my current project which demonstrates why I needed it.


[Edit] I'v just realised that this might have been better posted in the "Useful community Functions" thread. So I have posted it there too.

AGK V2 user - Tier 1 (mostly)

Attachments

Login to view attachments
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 9th Aug 2015 21:07
Ignore my code!

I works but it is slow and ugly.

Thanks to Batvink we now have a much simpler, faster and more elegant function:


AGK V2 user - Tier 1 (mostly)

Login to post a reply

Server time is: 2024-11-22 04:43:16
Your offset time is: 2024-11-22 04:43:16