Nanfeng

Notes on software development, code, and curious ideas

Refreshing Label Size in Cocos Creator

Immediately after changing Label text, its UITransform may still contain the previous dimensions. Waiting until the next frame works, but explicitly updating render data is clearer:

1
2
3
4
const label = node.getComponent(Label);
label.string = 'Updated text';
label.updateRenderData(true);
const size = node.getComponent(UITransform).contentSize;
+