I used a hacky way to get the size right. The issue here is that the Label widget will not immediately return the correct size after you have set the text. This is probably because the size calculations are deferred until the next _draw()
call.
mLabelObj.set_text(" ")
mLabelObj.set_size(Vector2(8, 8))
mLabelObj.set_text(pText)
mLabelObj.set_size(Vector2(8, 8))
var aLabelSize = mLabelObj.get_size()
mLabelObj.set_size(aLabelSize)
The trick here is that when you set the widget size to a very small value, the widget will resize to a minimum size which still allows it to display all the contents. The first pair of calls
mLabelObj.set_text(" ")
mLabelObj.set_size(Vector2(8, 8))
Resize the label to minimum possible size. Then I set the text and resized the label to a small size again. This will result in a label with a minimum size that will still display the text.
mLabelObj.set_text(pText)
mLabelObj.set_size(Vector2(8, 8))
var aLabelSize = mLabelObj.get_size()
However, sometimes, after the _draw()
call, the size of the Label is larger than aLabelSize
. To avoid that, set the size of Label again.
mLabelObj.set_size(aLabelSize)
Now you have the size of the label in aLabelSize
, and the label is also really of the same size. Hope that helps.