#include "uilabel.h" #include #include void UILabel::setup() { UIWidget::setup(); setFocusable(false); setAlign(AlignLeft); } void UILabel::render() { UIWidget::render(); Rect textRect = m_rect; textRect.setTopLeft(textRect.topLeft() + m_offset); m_font->renderText(m_text, textRect, m_align, m_foregroundColor); } void UILabel::setText(const std::string& text) { m_text = text; // auto resize if(!m_fixedSize && !m_rect.isValid()) { Size textSize = m_font->calculateTextRectSize(m_text); if(m_rect.width() <= 0) m_rect.setWidth(textSize.width()); if(m_rect.height() <= 0) m_rect.setHeight(textSize.height()); } } void UILabel::resizeToText() { resize(m_font->calculateTextRectSize(m_text)); } void UILabel::onStyleApply(const OTMLNodePtr& styleNode) { UIWidget::onStyleApply(styleNode); for(const OTMLNodePtr& node : styleNode->children()) { if(node->tag() == "text") setText(node->value()); else if(node->tag() == "align") setAlign(fw::translateAlignment(node->value())); else if(node->tag() == "offset") { setOffset(node->value()); } } }