I have a sf::Text with multiple lines how can I set its content alignment in the center sf::Text?
with just single line i can handle it with setOrigin
but with multiple lines i dont know how
Depends on what your object is. I would reccomend having the multiline object be a vector of multiple sf::Text objects (each Text object is a new line). You get the right size of the box you want your multiline text to be in and then you use origin on each line to center each text in its position. You can try something like this:
class MultiLine
{
public:
vector<sf::Text> texts;
int width;
int heigth;
int posx;//starting position of the multiline text block
int posy;
int heigthSpacing;
sf::Font font;
int widthSpacing;
MultiLine(int wid, int he) : width(wid), heigth(he)
{
//you can manipulate the size of the box manually via contructor or dynamically via the amount of lines and spacing between.
//this is example of contructor
//addvalues here, move to position here. position is important for later on as setorigin sets origin of point + pos.
//you can change the pos at any time later too, but the initial pos is important
}
void addLine(string text)
{
sf::Text tmp;
tmp.setCharacterSize(30);
tmp.setFont(font);
tmp.setString(text);
tmp.setPosition(posx+width,posy+texts.size()*heigthSpacing);
texts.emplace_back(tmp);
}
void centerText()
{
for(auto &v : texts)
{
v.setOrigin(v.getGlobalBounds().width/2, v.getGlobalBounds().height/2);
v.setPosition(posx+width/2,v.getPosition().y);
}
}
};