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

Author Topic: making a rectangle enclosing a text  (Read 1571 times)

0 Members and 1 Guest are viewing this topic.

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
making a rectangle enclosing a text
« on: May 26, 2015, 06:05:51 am »
float height = m_text.getFont()->getLineSpacing(m_text.getCharacterSize()) / 2.f;
m_rect.setSize({ m_rect.getSize().x, m_text.getLocalBounds().height + height});
window.draw(m_text); window,draw(m_rect);

when the text has letters it looks fine


but when the text is just like "..." the rectangle's height is shorter than it should be


how can i solve this issue?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: making a rectangle enclosing a text
« Reply #1 on: May 26, 2015, 08:19:13 am »
Not sure if this will fix it, but sf::Font::getLineSpacing() won't do what you're wanting, I think.
I'd make the height of m_rect simply equal to m_text.getCharacterSize().

That way, you get the actual size of a character, rather than the bounding rectangle (which changes if the text changes).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: making a rectangle enclosing a text
« Reply #2 on: May 26, 2015, 12:14:39 pm »
Bounds rectangle has non zero left and top. I've marked the actual bounds in your second image (with faint red). That should let you understand what's wrong easily.
Back to C++ gamedev with SFML in May 2023