diff --git a/src/ui/components/Button.cpp b/src/ui/components/Button.cpp index 9fcefb6..d9dab92 100644 --- a/src/ui/components/Button.cpp +++ b/src/ui/components/Button.cpp @@ -22,6 +22,18 @@ void Button::setText(const std::string& text, TextStyle style) { } } +void Button::setText(const std::string& text) { + m_buttonData.text = text; + + // 如果提供了 TextRenderer,则立即测量文本并更新控件尺寸 + if (m_textRenderer) { + auto [w, h] = m_textRenderer->getTextSize(text, m_buttonData.textstytle); + m_rect.w = static_cast(w); + m_rect.h = static_cast(h); + m_buttonData.rect = m_rect; + } +} + void Button::setBackgroundColor(SDL_Color normal) { m_buttonData.backgroundColor = normal; } diff --git a/src/ui/components/Button.h b/src/ui/components/Button.h index fc2ddc6..47b95c7 100644 --- a/src/ui/components/Button.h +++ b/src/ui/components/Button.h @@ -25,6 +25,12 @@ public: */ void setText(const std::string& text, TextStyle style); + /** + * @brief 设置按钮文本(不改变样式) + * @param text 按钮文本 + */ + void setText(const std::string& text); + /** * @brief 设置背景颜色 * @param normal 正常状态颜色