Scrollable attributed string component


#1

Sorry if this is a dumb question but I can’t seem to find an easy (out-of-the box) solution for this.

I want to display an EULA in my app. I thought I’d just use the TextEditor but I found out that it can’t handle attributed strings. I used Fabian’s fabulous RtfFileLoader to get an attributed string from an RTF file (https://github.com/hogliux/RtfFileLoader) but I can only draw it directly to the component which doesn’t work with long text.

Do I need to cook up something myself to display an AttributedString in a scroll view?
What do you suggest?


#2

just do a regular component which has paint() overridden to display the attributed string.
stick that component in a ViewPort with appropriate scroll bars turned on/off.

https://docs.juce.com/master/classAttributedString.html#a2befaf1fec54e78f844f034986f5f6f8

struct AttrStringViewer : public Component
{
    void paint(Graphics& g) override
    {
        attrString.draw(g, getLocalBounds());
    }
    AttributedString attrString;
};

#3

Yeah, that’s basically it. You need to add a size calculation method to set the size of the viewport’s scrolled component appropriately.

As this is really something many people will need, I was just thinking that I was missing something obvious.

Here’s what I came up with. I’m sure it can be improved but maybe it’s helpful for someone else looking for the same thing:


#4

you don’t need to mark those functions you’re overriding as virtual. they’re already virtual in the base class.


#5

Does it hurt if I do? Or do any harm to the performance?
I guess it just became a habit.


#6

It’s just bad code style, but it makes no difference to the compiler output.


#7

I guess I can live with that :wink: