Hi, like thousand people here, i'm trying to make a
button with a text centered inside.
I've been searching in the forum some solutions, but it seems useless for me.
The thing is, i,ve decided to center my text using the getLocalBounds() function.
Basically, this is my code:
int diffX = rect.width - txt.getLocalBounds().width;
int diffY = rect.height - txt.getLocalBounds().height;
float posX = rect.left + diffX / 2.f;
float posY = rect.top + diffY / 2.f;
txt.setPosition(sf::Vector2f(posX, posY));
txt.setColor(sf::Color::White);
Were
rect is the rectangle of the button and
txt is (obviously) my text.
I calculate de difference between both sizes and i set the text position leaving the half of the difference on each side.
The problem is that the
bounding box of the text it's perfectly aligned, but
text isn't.
This is how it looks:
(Note: i'm not sure if i'm uploading the image properly)
(Note2: sorry about ortography, i'm not English)
Thanks.