SFML community forums
Help => Graphics => Topic started by: texus on September 05, 2011, 08:15:02 pm
-
I am creating a button class for SFML2 and I am trying to center a text in my button (a texture).
The problem is that a text is not draw exactly at the correct coordinates (when drawing e.g. a text on position 0 then it will be drawn a little lower).
This is the code I am using:
sf::FloatRect rect = Text.GetRect();
rect.Left = (Texture.GetWidth() / 2.0f) - (rect.Width / 2.0f);
rect.Top = (Texture.GetHeight() / 2.0f) - (rect.Height / 2.0f);
Text.SetPosition(rect.Left, rect.Top);
Is there a way to get the top position of the text right?
At this moment the text is always too low.
-
This is because the first line is aligned vertically on the height of the tallest character -- even if it's not in your string. This is to keep the top of the string steady even if you add higher characters on the first line.
The exact computation is not straight-forward: you would have to iterate through all the characters of the first line, compute the highest character size, subtract this from the CharacterSize of your sf::Text, and subtract the result from the height of the bounding rectangle.
:D
-
Thanks, I will try that tomorrow.
-
I couldn't find a function that calculates the size of one character, but I finally found my solution in the code from sf::Text::UpdateRect.
I will post my code here in case someone needs the same function.
size_t CharacterSize = Text.GetCharacterSize();
sf::Font Font = Text.GetFont();
std::string String = Text.GetString().ToAnsiString();
bool bold = (Text.GetStyle() & sf::Text::Bold);
size_t MaxHeight = 0;
for (size_t x=0; x<Text.GetString().GetSize(); ++x)
{
sf::Uint32 Character = String.at(x);
const sf::Glyph& CurrentGlyph = Font.GetGlyph(Character, CharacterSize, bold);
size_t Height = CurrentGlyph.Bounds.Height;
if (MaxHeight < Height)
MaxHeight = Height;
}
sf::FloatRect rect = Text.GetRect();
rect.Left = (TextureNormal.GetWidth() / 2.0f) - (rect.Width / 2.0f);
rect.Top = (TextureNormal.GetHeight() / 2.0f) - (MaxHeight/2.0f) - (rect.Height-MaxHeight) + ((rect.Height-CharacterSize)/2.0f);
Text.SetPosition(rect.Left, rect.Top);
EDIT: The text was drawn too high but I have solved it and changed it in the code.
-
Hello,
I'm trying to do quite the same thing. But when I try that :
sf::Text Text;
sf::FloatRect rect = Text.GetRect();
MinGW says that :
class sf::Text' has no member named 'GetRect'
It seems to me that I wrote exactly the same line as you did.
So what's the problem ?
-
sf::Text Text;
sf::FloatRect rect = Text.GetGlobalBounds();
Global can be replaced by Local.
-
This works. Thank you.
But could anyone also explain me why this :
sf::Text Text;
sf::FloatRect rect = Text.GetRect();
doesn't work please ?
-
This function was removed.
-
This function was removed.
Is there an alternative? I'd really like to center my text :P
-
I think Zinlib gave you the solution, either GetGlobalBounds() or GetLocalBounds() depending on what you want to do :)