Auto center text from a dynamic vector string

Hey everyone,

Does anyone know if it is possible to auto center the text of a dynamically changing string? What I have happening is there is a set of strings stored in a vector and each one is a different size. What I want to do is somehow have them centered in the screen when they change. Right now my code is this:

bebas2.drawString(titles[titleCount], ofGetWidth()/2, 300);

There are some that look fine with that, however since it is using ofGetWidth() it of course places it at the half point of the screen.

Hey Brian!

As long as the string is a single line. (as I don’t know the behaviour for multi-line texts)
You can do the following:
bebas2.drawString(titles[titleCount], (ofGetWidth() * 0.5f) - (bebas2.getStringBoundingBox(titles[titleCount], 0, 0) * 0.5f), 300);

You first put the string in the middle of the screen with “getWidth times a half”. Then subtract half of the width of the generated string.

Kind Regards,