Created
January 16, 2019 18:53
-
-
Save guitarflow/e2a237658956980a3958ff6f799fc197 to your computer and use it in GitHub Desktop.
Scrollable AttributedString component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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