SFML community forums

Help => Graphics => Topic started by: lampcord on June 04, 2015, 01:33:37 am

Title: Text alignment issues
Post by: lampcord on June 04, 2015, 01:33:37 am
I'm having a little trouble with text alignment. So I did a little experiment and saw some confusing results.

I decided to draw a rectangle using the position of the text and the bounding rect of the text and then drawing the text on top of it. I was surprised to see that although the rectangle appeared to be the same size as the text they did not align correctly.  The text appeared about 2 pixels to the right and 8 pixels down from rectangle. So I checked the origin of the text and it is set at 0,0 which I assume is the upper left corner.  Here's a snippet of my code if that helps:


text.setPosition(x, y);
sf::RectangleShape rec2;
rec2.setFillColor(sf::Color::White);
sf::FloatRect textsize = text.getLocalBounds();
rec2.setSize(sf::Vector2f(textsize.width, textsize.height));
rec2.setPosition(x,y);
rw.draw(rec2);
rw.draw(text);

I expected the text to be exactly bounded by the rectangle. Is there something obvious I'm missing? I can adjust by setting the text origin to -2,-8 but that seems kind of strange. Its almost as if they are using different coordinate systems but I'm not sure.

Title: Re: Text alignment issues
Post by: G. on June 04, 2015, 01:45:32 am
Yes, top and left localBounds of sf::Text usually are not 0 and 0.
Title: Re: Text alignment issues
Post by: eXpl0it3r on June 04, 2015, 01:46:09 am
Similar topics have come up again and again, I suggest you search the forum a bit, it's very likely you'll find an answer. ;)