Skip to content

Instantly share code, notes, and snippets.

@guitarflow
Created January 16, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarflow/e2a237658956980a3958ff6f799fc197 to your computer and use it in GitHub Desktop.
Save guitarflow/e2a237658956980a3958ff6f799fc197 to your computer and use it in GitHub Desktop.
Scrollable AttributedString component
#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);
}
virtual void paint(Graphics& g) override
{
g.setColour(Colours::black);
if (_attributedString.getText().length() == 0)
return;
_drawingLayout.draw(g, Rectangle<float>(_spacing, _spacing, getWidth(),
getHeight()-2*_spacing));
}
virtual void resized() override
{
Component::resized();
this->refreshSize();
}
const int _spacing = 10;
AttributedString _attributedString;
Viewport* _parentViewport;
TextLayout _drawingLayout;
};
//----------------------------------------------------------------------//
// ScrollableTextView
//----------------------------------------------------------------------//
#pragma mark ScrollableTextView
ScrollableTextView::ScrollableTextView()
{
ScrollableTextContainerView* container = new ScrollableTextContainerView();
this->setViewedComponent(container);
container->setBounds(0, 0, getWidth(), getHeight());
container->_parentViewport = this;
}
ScrollableTextView::~ScrollableTextView()
{
delete getViewedComponent();
}
void ScrollableTextView::paint(Graphics& g)
{
g.fillAll(Colours::white);
}
void ScrollableTextView::resized()
{
Viewport::resized();
ScrollableTextContainerView* container = (ScrollableTextContainerView*) getViewedComponent();
container->refreshSize();
}
void ScrollableTextView::setAttributedString(const AttributedString& string)
{
ScrollableTextContainerView* container = (ScrollableTextContainerView*) getViewedComponent();
container->_attributedString = string;
container->refreshSize();
}
class ScrollableTextView : public juce::Viewport
{
public:
ScrollableTextView();
virtual ~ScrollableTextView();
void setAttributedString(const juce::AttributedString& string);
private:
juce::AttributedString _attrString;
virtual void paint(juce::Graphics& g) override;
virtual void resized() override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment