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 (GitHub - hogliux/RtfFileLoader: A tool to convert rtf files to an open-source xml format which can then be used to create a JUCE AttributedString ) 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?
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;
};
1 Like
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:
ScrollableTextView.cpp
#include "ScrollableTextView.h"
struct ScrollableTextContainerView : public Component
{
void refreshSize()
{
int width = _parentViewport->getWidth() - 2*_spacing;
_drawingLayout.createLayoutWithBalancedLineLengths(_attributedString, width);
this->setSize(width, _drawingLayout.getHeight() + 2.0*_spacing);
}
This file has been truncated. show original
ScrollableTextView.h
class ScrollableTextView : public juce::Viewport
{
public:
ScrollableTextView();
virtual ~ScrollableTextView();
void setAttributedString(const juce::AttributedString& string);
private:
This file has been truncated. show original
you don’t need to mark those functions you’re overriding as virtual. they’re already virtual in the base class.
Does it hurt if I do? Or do any harm to the performance?
I guess it just became a habit.
It’s just bad code style, but it makes no difference to the compiler output.
I guess I can live with that