From 4a71e4f7421d4458831679fda5f00388ab0211ee Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 13 Dec 2025 18:35:07 +0800 Subject: [PATCH] Added a overload setText --- src/ui/components/Button.cpp | 12 ++++++++++++ src/ui/components/Button.h | 6 ++++++ 2 files changed, 18 insertions(+) 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 正常状态颜色