Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Text alignment issues  (Read 1347 times)

0 Members and 1 Guest are viewing this topic.

lampcord

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Text alignment issues
« 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.


G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Text alignment issues
« Reply #1 on: June 04, 2015, 01:45:32 am »
Yes, top and left localBounds of sf::Text usually are not 0 and 0.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Text alignment issues
« Reply #2 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/