Looking at the documentation it seems like the function
getLocalBounds
could be of use to you. The line would be:
float width = text.getLocalBounds().width;
I'm not sure if the sf::Text
object would add any padding on the ends of the bounding rectangle.
Alternatively, you could make use of findCharacterPos
with something like:
float width = text.findCharacterPos(numChars - 1).x - text.findCharacterPos(0).x;
where numChars
is the number of characters in the string of your text
object. However, since findCharacterPos
will return global coordinates, it's probably more convenient to use getLocalBounds
, this way you don't have to worry about whether your text
object has any transformations applied to it.