Custom width of Vaadin GridLayout - alignment

I'm working with a 2x2 GridLayout in Vaadin.
gridLayout = new GridLayout(2, 2);
gridLayout.setWidth(100, Unit.PERCENTAGE);
gridLayout.setMargin(true);
gridLayout.setSpacing(true);
The cell in the upper-left corner contains a simple label aligned to the right. Upper-right cell contains a textfield aligned to the left. The second row simply contains a label below the textfield.
gridLayout.addComponent(captionLabel, 0, 0);
gridLayout.addComponent(inputField, 1, 0);
gridLayout.setComponentAlignment(captionLabel, Alignment.MIDDLE_RIGHT);
gridLayout.setComponentAlignment(inputField, Alignment.MIDDLE_LEFT);
Now I want to have both columns within the grid to have size set to 50% to have the whole layout aligned in the middle of my page - now it is slightly shifted to the left side and I can not figure out why...
Vaadin's wiki-page shows a related article, but I can not figure out how to work with it. Seems to be deprecated, because I can not access #getColumn(); - method?! https://vaadin.com/wiki/-/wiki/10674/Configuring+Grid+column+widths
For further info: The GridLayout is added as a separate component to a VerticalLayout.

You can influence the column width's with the grid.setColumnExpandRatio(1, 1); method.
If you wish to have both use 50% of the total width, just set the expand ration to the same value on both columns.
Please also note this:
A layout that contains components with percentual size must have a defined size!
If a layout has undefined size and a contained component has, say, 100% size, the component would fill the space given by the layout, while the layout would shrink to fit the space taken by the component, which is a paradox. This requirement holds for height and width separately.
Book of Vaadin

Solved it by adding a HorizontalLayout-wrapper for the left column containing the label. The right colum contains a VerticalLayout with all other components.
Label captionLabel = new Label(localized);
captionLabel.setSizeUndefined();
HorizontalLayout wrapper = new HorizontalLayout();
wrapper.setSizeFull();
wrapper.addComponent(captionLabel);
wrapper.setComponentAlignment(captionLabel, Alignment.TOP_RIGHT);
[...]
gridLayout.addComponent(wrapper, 0, 0);
gridLayout.addComponent(inputLayout, 1, 0);
gridLayout.setColumnExpandRatio(0, (float)0.5);
gridLayout.setColumnExpandRatio(1, (float)0.5);

Related

Find the lowest positioned element

I'm trying to make constraints via Snapkit in a table view cell but my problem is I need to find out which element has max y position (The lowest one).
I have an UIImageView and next to image view UILabel elements. The label text is dynamic and could be very long or very short. Below these 2 elements, I have another one that should be aligned based on the label height, either taking image view or label.
My question is how to find which element (UIIMageView, UILabel) has a bigger Y position.
To be more clear I attached a draw with simple two cases.
Set a greaterThanOrEqualTo constraint on both elements.
In "plain language":
AnotherElement.top >= ImageView.Bottom (with constant of 12, or however much space you want)
AnotherElement.top >= Label.Bottom (with constant of 12, or however much space you want)

Content of Grid list to fill the height

I am placing content in the grid list component and I want the grid tile to grow accordingly to the content it has. I tried to use rowHeight="fit" as documentation states but it doesn't seems to work.
Here an example
MatGridList overall size is directly related to the rowHeight setting. The default rowHeight value is a ratio 1:1 (which means column width equals row height).
When you use a ratio, the list will have a fixed width/height ratio overall, so if the list can't grow in one direction because of window or layout restrictions, it can't grow in any direction. This is why your example looks the way it does. If you expand the width of the window, you'll see the list expand vertically as well. You can set a different rowHeight value to make the tiles taller than wide such as rowHeight="1:2" (that seems backwards to me but that's how it works).
When you use rowHeight="fit", this doesn't fit the list to the content, it fits the row heights to the list height, but as noted in the documentation, you must set a height on the list or a parent for this to work properly.
Setting a fixed value for rowHeight does what it sounds like.

Vaadin Table columns autosize/autoscale without horizontal scrollbar

I have a vaadin table, with say 5 columns.
In the last column (right-most) we have the edit/delete buttons which always take up the same amount of width.
When I set the width of the last column to a specific width, and all other columns to a specific setColumnExpandRation(10); then everything is ok, with the exception that the normal columns are not autoscaled.
When I the the last column to a specific width, and all other columns to autoscale, (setColumnWidht(-1)), then the normal columns are sized depending the content, but the whole table width is too large and shows a horizontal scrollbar.
Example of such a case in the picture below
And the java code:
Table tb= new Table();
tb.setWidth(100, Unit.PERCENTAGE);
tb.setHeight(100, Unit.PERCENTAGE);
layout.addComponent(tb);
BeanItemContainer<Person> pList =
new BeanItemContainer<>(Person.class);
for (int i=0; i < 20; i++)
{
Person p1= new Person("******", "++++++++++", "--++--++--++--++--++--++", "????", "3333 "+i, new Date());
pList.addBean(p1);
}
tb.setContainerDataSource(pList);
tb.setColumnWidth("zip", 20);
tb.setColumnExpandRatio("firstName", 1);
Is there a way to have the last column with a fixed width, but still have the other columns auto sized, to have widths depending the content?
The solution is to set table to be 100% width.
Then to set last column to have fixed widht.
Set the only one of the other columns to have expand ratio 1, to force other columns to get proper space.
Container which contains table must have 100% width.
If you get scrollbars then you must wrap text inside table.

What do "Use standard value” and “Constrain to Margins" mean in Auto Layout?

I have gone through a couple of Auto Layout tutorials such as this. However I am still not clear on what the following options do in the pin dialog
What are the differences between standard value, manual values, and canvas values?
What does the constrain to margin checkbox do?
What does align do?
What are the differences between standard value, manual values, and canvas values?
Standard value uses "the recommended spacing for constraints that specify distance between items", which is usually around 10 points.
Current canvas value copies the value from how you have the objects currently displayed on the canvas.
Manual values are whatever you want.
What does the constrain to margin checkbox do?
This constrains to a container view's margins instead of its edges. From the docs:
“Horizontal and vertical constraints to a container view can be to the margin or to the edge. Margins correspond to the values in the layoutMargins atttribute of UIView and specify recommended minimal distances between an edge of a container view and the corresponding edge of a child.”
You can set a view's margins using the layoutMargins property.
What does align do?
This creates a constraint that edges or center of one view should be aligned with edges or center of another view. For example, in a column of text views, you might want every text field to have their leading and trailing edges aligned.

Determine UILabel height for centering when using adjustFontSizeToFitWidth

I have the following cell design where the numeric label shrinks and the "Overall" label is directly underneath.
I have properly set the adjustFontSizeToFitWidth and minimumFontSize properties. The font is resizing correctly. However, anchoring the numeric label to the bottom is challenging. Particularly when the font shrinks the gap between the two labels widens and does not appear vertically centered.
I have tried sizeToFit, sizeThatFits, and using the font's pointSize. All unsuccessfully.
I am aware of sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:, but don't understand why I would need it in combination with adjustFontSizeToFitWidth.
Ah, so you want to position the UILabels in the middle of the container view (both horizontally and vertically)?
I have rephrased my answer so it will make more sense to future readers.
My code is assuming that you have the 3 IBOutlets set up:
UIView *containerView; //Your nice view containing the two textfields.
UILabel *points; //The label containing the number.
UILabel *overall; //The textfield containing the text 'overall'.
You could simply set the frame of the labels after assigning the text and calling the sizeToFit.
This first line positions the UILabel points, the only change being that the y coordinate is half of containerView subtract half of the height of itself.
points.frame = CGRectMake(points.frame.origin.x, (containerView.frame.size.height / 2) - (points.frame.size.height / 2), points.frame.size.width, points.frame.size.height);
To position the overall accordingly - say there is a distance of say 6 between the number and overall labels:
int space = 6;
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
Having read your comments, I think you are after this solution. If you want both UILabels to appear in the middle; subtract (overall.frame.size.height / 2) + (space / 2) from the y value of points like so (with the code of number 2 beneath it):
int space = 6;
points.frame = CGRectMake(points.frame.origin.x, ((containerView.frame.size.height / 2) - (points.frame.size.height / 2)) - ((overall.frame.size.height / 2) + (space / 2)), points.frame.size.width, points.frame.size.height);
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
The final point will produce an output like this image. As you can see the blue line is half of the whole image, and intersects the black rectangle (which is snuggly around the two labels) at its middle point. I hope this is what you were after.
Instead of using two labels, use CATextLayer instead. You will be easily able to make one part BOLD and the other normal. plus position and adjusting size for One layer will be easy relative to placing two labels. shadow setting, line break mode, fonts you will be able to adjust everything beautifully :)

Resources