Latest Uploads
Mutant Mon ... (Blitz3D)

steve_ancell

Mutant Mon ... r package.

steve_ancell

image-13-0 ... -01-38.jpg

Jayenkai

Mutant Monty Remake

steve_ancell

hai.jpg

CodersRule

SillyAddition.png

Jayenkai

Forum Home

Text IN CENTER!

UserMessage
Posted : Thursday, 27 October 2011, 15:25 | Permalink | Mark Here
Afr0


WW Entries : 3
Does anyone have a bulletproof mathematical/algorithmical approach for centering text against a component (say, a button) given the component's x, y, width, height and the text's width and height?

I obviously don't know the text's x and y, as those are the parameters I'm trying to figure out... I want them to be centered!

-----
Afr0 Games

Project Dollhouse on Github - Please fork!
Posted : Thursday, 27 October 2011, 15:34 | Permalink | Mark Here
HoboBen


WW Entries : 9
Off the top of my head... (untested)

-->


To explain, take
textx = x + ((w - textw) / 2);

The x being the offset of the component is simple. We can effectively ignore this for now by pretending it's zero, leaving:

text = (w - textw) / 2;

This is
w/2 (the mid-point of the component)

Subtracted by
textw/2 (half the graphical length of the string)

Which will centre it about the mid point.

Which simplifies to (w/2) - (textw/2) = ((w - textw)/2)



-----
github
Posted : Thursday, 27 October 2011, 16:11 | Permalink | Mark Here
JL235


WW Entries : 7
I believe Ben's answer is right (although I've not run it).

But bear in mind that you can't centre text just using it's pixel height. The problem is that certain characters are meant to extend way up or below, and so become out of place.

Essentially, the centre of a character is probably not half it's height in pixels.

This image from the W3 Spec on HTML5 Canvas is a good illustration of what I mean:


-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Posted : Thursday, 27 October 2011, 20:49 | Permalink | Mark Here
Afr0


WW Entries : 3
Well I'm using Comic Sans for rendering fonts (as that is more or less what the original TSO used), and I'm also not mixing uppercase and lowercase letters right now (as that would require rendering two different fonts at the same time for the desired effect), so I should be relatively fine.

Thanks Hobo!

-----
Afr0 Games

Project Dollhouse on Github - Please fork!
Posted : Friday, 28 October 2011, 04:36 | Permalink | Mark Here
spinal


Comic Sans!!!!!!!!!!!!!!!!!!!!1111111111111111111 The entire world should have already told you not to use comic sans for anything ever!!

Does whatever language you're using not support centered text already?

-----

3DS - 1762-2660-3218
Posted : Friday, 28 October 2011, 05:42 | Permalink | Mark Here
Afr0


WW Entries : 3
I know Comic Sans isn't a very popular font, but it worked nicely for The Sims and TSO.

Yes, the SpriteFont class in XNA has a MeasureString() function, but I still needed to wrap my head around the maths.

-----
Afr0 Games

Project Dollhouse on Github - Please fork!
Posted : Friday, 28 October 2011, 05:54 | Permalink | Mark Here
spinal


I would only worry about centring the width, keep the hight exactly the same for all button text, it will look out of place otherwise.

-----

3DS - 1762-2660-3218
Posted : Friday, 10 August 2012, 12:45 | Permalink | Mark Here
Afr0


WW Entries : 3
Ugh.... somehow I managed to mess this up again when putting scaling into the mix!

My code:

-->

This works for some buttons (where ScaleX and ScaleY is 0), but not for others!
Please keep in mind that GlobalScale is the global scalefactor (I.E the result of the current resolution's width by 800), and ScaleX and ScaleY is the button's individual scalefactor.

Any tips or hints are greatly appreciated!

-----
Afr0 Games

Project Dollhouse on Github - Please fork!
Posted : Sunday, 12 August 2012, 18:15 | Permalink | Mark Here
dna


The code you wrote works ben.

There just needs to be some conversion for txt.

Using Stringwidth(txt) somewhere inside of CalculateTextW(txt)
should do it.
Than you would have both the x and textw in the same terms, pixels.

-----
DNA
Posted : Monday, 13 August 2012, 01:30 | Permalink | Mark Here
Afr0


WW Entries : 3
I'm aware that Hobo's code works, but I made my equation a little more difficult by throwing scaling into the mix...

I made it work last night, it turned out I had my braces messed up;

-->

To explain, I had;

-->

It's amazing what a couple of braces can do!

-----
Afr0 Games

Project Dollhouse on Github - Please fork!
Latest Posts
Mutant Monty update.
steve_ancell Wed 09:41
Android Dev Test Doohickey
Jayenkai Tue 09:58
Aminal Xing Knew Leef
Jayenkai Tue 04:25
Spark
dna Mon 17:24
HoboTile
steve_ancell Sun 10:07
Hoover
steve_ancell Sat 21:53
Scrabble Rabble
Jayenkai Fri 08:13
Not Fit for Purpose
Jayenkai Thu 20:29
Happy Birthday, Krakatomato
steve_ancell Thu 18:03
How Deep is Your Code?
Hotshot Thu 14:35
More

Latest Items
Blog : Snow Worklog
Cower Mon 12:23
Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
steve_ancell Sat 19:12
Showcase : MLG : Microscopic Lifeform Genocide
rockford Wed 09:24
News : Newsletter #176
Jayenkai Sat 04:49
News : Newsletter #175
Dabz Tue 09:38
Blog : Snow: More Material Junk
Cower Sat 23:17
Techy : AppleTV
Jayenkai Thu 09:40
Blog : Graphviz
steve_ancell Sat 14:17
Pets : Top-Down Shadow Hack
Jayenkai Tue 05:52
Snippet : JNKrunch v1.0
Jayenkai Sat 07:20
News : Newsletter #173
waroffice Fri 04:47
Blog : Material Loading
Cower Fri 02:08
Pets : I Done Won A Thing
shroom_monk Sun 11:31
Pets : Repurposing A Lexer
Cower Mon 22:06
Bah : Feeling a Little Angry
spinal Mon 11:26
More

Who's Online
Evil Roy Ferguson
Wed, at 21:24
dna
Wed, at 20:23
therevillsgames
Wed, at 20:19
steve_ancell
Wed, at 19:36
Jayenkai
Wed, at 18:28
CodersRule
Wed, at 18:09
9572AD
Wed, at 17:04
HoboBen
Wed, at 15:50
Stealth
Wed, at 15:41
Hotshot
Wed, at 14:49
Link to this page
Site : Jayenkai 2006-Infinity |
MudChat's origins, BBCode's former life, Image Scaler.