From da48649370e4c8641962001eb2940445f8a1f169 Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 10 Oct 2014 11:01:51 +0800 Subject: [PATCH] fix button title only content size issue --- cocos/ui/UIButton.cpp | 5 +++++ .../UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cocos/ui/UIButton.cpp b/cocos/ui/UIButton.cpp index 8e938366c4..0edd37554a 100644 --- a/cocos/ui/UIButton.cpp +++ b/cocos/ui/UIButton.cpp @@ -638,6 +638,11 @@ void Button::setPressedActionEnabled(bool enabled) void Button::setTitleText(const std::string& text) { _titleRenderer->setString(text); + if (_ignoreSize) + { + Size s = getVirtualRendererSize(); + this->setContentSize(s); + } } const std::string& Button::getTitleText() const diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp index bdaa38e9fc..32f2b607e7 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp @@ -37,6 +37,8 @@ bool UIButtonTest::init() // Create the button Button* button = Button::create("cocosui/animationbuttonnormal.png"); + CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, + button->getContentSize().height); button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this)); button->setZoomScale(0.4); @@ -632,7 +634,10 @@ bool UIButtonTextOnly::init() // Create the button auto button = Button::create(); button->setNormalizedPosition(Vec2(0.5, 0.5)); + button->setTitleText("PLAY GAME"); + CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, + button->getContentSize().height); button->setZoomScale(0.3); button->setPressedActionEnabled(true); button->addClickEventListener([this](Ref* sender) {