Text Aligned String (via std::setw(), std::left) Renders Differently on UI


Hi all :slight_smile:

I’ve got a lot of audio measurement data I’d like to write to a simple log.txt file from my program. I’d also like to display this text in the UI.

I’m using the stream manipulators std::setw() & std::left & std::right on stringstream objects to align the values and make it look nice.

The log file looks as how I’d expect, as well as outputting the string in the debugger:

However, when I try to draw it in my UI using TextLayout::draw() or AttributedString::draw(), the text doesn’t align:

Is there something I’m missing here? I’ve been digging around the API for a bit now.

Also, if anybody knows a better way to format text in this manner that would certainly put a cold compress on this mess of tedious text aligning.



Try String::paddedRight() and String::paddedLeft()



Ah, that’s very handy thanks for the tip! The code is a lot cleaner but the drawn text is still not aligned correctly:


Ok I see what’s going on here. In the debugger all the characters have the same width in pixels, but in the UI they don’t.

Here’s the UI output with the fill chars set to ‘X’:

So it looks like I need to use a monospaced font. Setting the font to Font::getDefaultMonospacedFontName () does the trick :slight_smile: although the text isn’t quite as beautiful: