libgdx - scene2d.ui label position - libgdx

How can I dock a multiline Label to the bottom of the screen? There's the Label.setY() method but it works in some arcane way that makes no sense to me. I can get the approximate height of the label with getWrappedBounds() but I can't figure out how to use the label's height to set the position of the label.

You cannot position directly a label, you would have to add to a class that is using WidgetGroup such as Table. Or you can a Container [that is supposed to be more lightweight than a Table] and add the label to it:
Label testLabel = new Label("testme",super.getSkin(),"gameName");
Container labelContainer = new Container(testLabel);
labelContainer.bottom();
Then the label should be displayed at the bottom

You should prepare your label first and then place it:
skorTextLabel = new Label("Score " + level_skoru, skorTextStyle);
skorTextLabel.setPosition(
ekran_genisligi * 0.5f - skorTextLabel.getWidth() * 0.5f,
skorTextLabel.getHeight() * 0.3f);
"skorTextLabel.getHeight() * 0.3f" distance from bottom.

Related

Changing label height property dynamically

I have a line of code:
Label11.Style["Height"] = (Label11.Height.Value + 15).ToString() + "px";
I populate the Label with text, each time I'm doing it I would like to increase the height of the Label by 15 px.
After the first populate it works fine (Height = 30), but after that it didn't change.
What am I doing wrong?
Finally, I managed to find the solution:
Label11.Height = new Unit((int)Label11.Height.Value + 15);

trying to set the text above the Image in ImageTextButton on libGdx

i'm using LIBGDX.
i'm trying to set a text string in the center of an image, and use it as a clickable button.
unfortunately i didn't find a solution for it.
Yes you can with the getLabel method:
myButton.getLabel().setAlignment(Align.center);
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Align.html
By default, ImageTextButton contains 2 Actors in 1 row: Image then Label. To change position of the Label one may rebuild the underlying table cells just after creation of ImageTextButton:
new ImageTextButton(...) {
val label = getLabel
val image = getImage
clearChildren()
add(image)
row() // second row
add(label)
}
Now Label will be under the Image. Code is Scala.

Scene2d - absolute positioning

I want to add 3 numbers next to each other on the same row in a table, I also want there to be about 100 units padding between each number. Under these numbers are words that correspond to them, for example:
0 0 0
Total Wins Losses
However, if the I use padding, ie table.add(actor).padLeft(100);, then changing the number from say 0 to 160 would also move the number to the right, since I'm using padding. example:
160 0 0
Total Wins Losses
How can I avoid this behaviour? It must be a common scenario.
I suggest to create a custom class that extends the WidgetGroup.
Inside this class you manage two labels, one for the number and one for the "label" of the number.
Then you can easily set the relative position of the labels respect each other without keeping in mind where they are positioned inside the tabel.
Maybe the constructor of this special class could accept a String for the "label" and the initial value of the number.
Tell me if the idea it's clear or if you need more info.
Luca
What I would do is set all the cells in the table to be centred, by default, with a little padding on left and right, as follows:
Table table1 = new Table();
// Set defaults for all cells in table. Alignment and padding in this case.
table1.defaults().align(Align.center).pad(0, 20, 0, 20);
Label number1 = new Label("160", skin);
Label number2 = new Label("0", skin);
Label number3 = new Label("0", skin);
table1.add(number1);
table1.add(number2);
table1.add(number3);
table1.row();
Label label1 = new Label("Total", skin);
Label label2 = new Label("Wins", skin);
Label label3 = new Label("Losses", skin);
table1.add(label1);
table1.add(label2);
table1.add(label3);
table1.row();
Try that.

GWT Label alignments and CSS

I am having some trouble with GWT Labels and html/css.
I have a horizontalPanel, and I am trying to align the 2 labels so that each 1 is on an end of the Horizontal Panel
1) [Label 1 --~500px-- Label 2]
However, it seems that the 500 px is divided up into 250px each for a label, and theres a white space. (First I tried to make it like like this)
2) [Label 1 Label 2 --~500 whitespace px-- ]
but what happens is this
3) [Label 1 -~250 whitespace px- Label 2 -~250px whitespace-- ]
I would like It to be like 2) so then i can set the HorizontalAllignment on the Horizontal Panel to ALIGN_JUSTIFY which would put on one each end. I have no idea how to do it. I tried using display: inline but that doesnt seem to work as I hoped (it makes the border just go around the text but the whitespace is there)
Here is the code I am messing around with .border is just (border: 1px solid Red) so i can visualize how the border and size is calculated
HorizontalPanel p = new HorizontalPanel();
p.setWidth("500px");
//p.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
//p.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_JUSTIFY);
Label l1 = new Label("Label 1");
Label l2 = new Label("Label 2");
l1.addStyleName("border");
l2.addStyleName("border");
p.add(l1);
p.add(l2);
I just threw this together from memory, give it a try and let me know if it works like you expect.
HorizontalPanel p = new HorizontalPanel();
p.setWidth("500px");
p.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_JUSTIFY);
Label l1 = new Label("Label 1");
Label l2 = new Label("Label 2");
l1.addStyleName("border");
l2.addStyleName("border");
p.add(l1);
p.setCellHorizontalAlignment(l1, HasHorizontalAlignment.ALIGN_LEFT);
p.add(l2);
p.setCellHorizontalAlignment(l2, HasHorizontalAlignment.ALIGN_RIGHT);
If that doesn't work, have you tried setting text-align:right; in your css for the second label?

How to get a RadioButton's label height - AS3

I want to get radioButton's label height in as3.
I have 5 radio buttons in my radioButton group. One of those radioButtons has a three line label, another has a single line etc...
When I try to access the radioButton.height, I always get same value regardless of the label height.
I need to know the height of the label, so I can set the radioButton y coordinate accordingly.
can I change the radioButton's label height?
spacing = 30;
rb.label = tempQQString //from xml. long or short string anyone
rb.group = myGroup;
rb.value = i + 1;
rb.x = answerX;
rb.y = questionField.height + (questionY+20) + (i * spacing); // here use rb.height or rb.textField.height
trace("rb height**" + rb.height) // always get velue 22;
addChild(rb);
RadioButton and any fl control that extends labelButton (all the ones that have a label), expose the actual text field with the .textField property.
So for your example, instead of using rb.height, you would use rb.textField.height to get the height of just the label portion of the control.
You can also set properties on the textField as well.
rb.textField.background = true;
rb.textField.backgroundColor = 0xDDDDDD;
rb.textField.multiline = true;
rb.textField.wordWrap = true;
rb.textField.autoSize = TextFieldAutoSize.LEFT;
now, for your scenario, you may be better off just using the radio button's bounds instead, as it will be the REAL height of the object.
rb.getBounds(rb).height; //this will be the true height of the component.
If your label was one line and your icon was taller than your label, you could potentially be getting a value from rb.textField.height that is smaller than the real height of the radio button.
Try this.
for(var i=0;i<rb.numChildren;i++){
if(rb.getChildAt(i) is TextField){
var txt:TextField = rb.getChildAt(i) as TextField;
trace(txt.height+":"+rb.height);//traces the height of text field and radio button
}
}