2D Unity: expand UI element based on text to the bottom - unity3d

I've got a game object (InfoBox) with a..
Vertical Layout Group (Padding 20 on all sides, Control Child Size Height active)
an image
a Content Size Fitter (Vertical Fit set to Preferred Size, Horizontal Unconstrained)
Inside this InfoBox is a Text-Element and every time I update the text, I want the InfoBox to expand or collapse, but to the bottom only. Right now my InfoBox always expands or collapses to both top and bottom, but I don't want that to happen. How can I achieve that? I added some screenshots to better visualise this.
These are my settings for the InfoBox:
And these are my settings for the text:
And this is what happens when I use for example less text, the box collapses from bottom and top, but I want it to stay fixed at the top (on the same line as the green box next to it) and only collapse on the bottom:
Any help is really appreciate!

You main issues looks like you are using a ContentSizeFitter also on the parent object. So it depends how this parent object is aligned with its according parent.
This sentence sounds strange I know but what you did is basically shifting up the alignment responsibility to the parent.
=> Make your UI element aligned to the top and grow downwards.
All you need to do for this is either set
Anchors Min y -> 1
Anchors Max y -> 1
Pivot y -> 1
or simply go to the RectTransform Inspector, open the alignment tool and hold Shift + Alt and click on the top alignment:
For the second instead of using the ContentSizeFitter I prefer to have my own script that only does what it should and nothing more.
You can get the preferredHeight which is exactly the value you are looking for.
The following script simply always applies the preferred height to the text rect. Using [ExecuteAlways] this also works in edit mode. Simply attach it on the same GameObject as the UI.Text component
[ExecuteAlways]
[RequireComponent(typeof(Text))]
[RequireComponent(typeof(RectTransform))]
public class TextHeightFitter : MonoBehaviour
{
[SerializeField] private RectTransform _rectTransform;
[SerializeField] private Text _text;
private void Update()
{
if (!_text) _text = GetComponent<Text>();
if (!_rectTransform) _rectTransform = GetComponent<RectTransform>();
var desiredHeight = _text.preferredHeight;
var size = _rectTransform.sizeDelta;
size.y = desiredHeight;
_rectTransform.sizeDelta = size;
}
}

Related

FreeText Annotation Appearance Stream In Landscape PDF Using iText

This is my code to create appearance stream for a free text annotation.
cs.rectangle(bbox.getLeft() , bbox.getBottom(), bbox.getWidth(), bbox.getHeight());
cs.fill();
String[] text = new String[1];
text[0] = "BAC"
cs.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), pdfJSAnnotation.getFontSize());
cs.beginText();
cs.setLeading(fontSize + 1.75f);
cs.moveText(0, bbox.getHeight() - fontSize + .75f);
for (String s : text) {
if (s.equals("\n"))
cs.newlineText();
else
cs.showText(s);
}
cs.endText();
where cs is PdfAppearance, bbox is Rectangle. This works okay when pdf is portrait. however, im having problems when it is in landscape, say if page rotation is 270.
The text shown is vertical. and even if i use cs.transform() to rotate, it does not even rotate properly. I also tried to save the state, do a rotate then display text and then call cs.restoreState() after cs.endText() but the outcome is still not correct.
any ideas?
the rectangle is correct since the 1st 2 lines where it fills a rectangle shape is correctly displayed. it is the text i am having problems with.
The solution to this is:
- set appearance dimension to (height,width) since it is 270 degrees.
Then in the PdfAppearance object:
translate it to (height, 0);
rotate to 270
translate to (-height,-height);
(Tried to remove this post but there is no option available)

Make Micro Chart – Harvey Ball size bigger in the sap.m.CustomTile

I would like to get bigger size of sap.suite.ui.microchart.HarveyBallMicroChart nested in the sap.m.CustomTile than is max "L" size:
var oChart = new sap.suite.ui.microchart.HarveyBallMicroChart({
size: sap.m.Size.L,
});
I would like to stretch out chart to the bottom and right side of the tile. Now it is (demo):
Thanks for any hint. I was also playing with the height and width property of HarveyBallMicroChart but nothing changed.
The poroperty I was looking for was isResponsive.

Trying to align text in center of rectangle while drawing freeText annotation but text goes in top left corner

I am trying to free text Annotation string align in center of rectangle but always set in top left corner
PdfContentByte pcb = stamper.getOverContent(page);
PdfAnnotation annotation = PdfAnnotation.createFreeText(stamper.getWriter(), rectangle, "Mayank Pandey", pcb);
annotation.put(PdfName.Q, new PdfNumber(PdfFormField.Element.ALIGN_MIDDLE));
Text showing left top corner in pdf:
You set
annotation.put(PdfName.Q, new PdfNumber(PdfFormField.Element.ALIGN_MIDDLE));
The constant Element.ALIGN_MIDDLE is defined as
/**
* A possible value for vertical alignment.
*/
public static final int ALIGN_MIDDLE = 5;
But the value of Q in free text annotations is specified as:
Q
integer
(Optional; PDF 1.4) A code specifying the form of quadding (justification) that shall be used in displaying the annotation’s text:
0 Left-justified
1 Centered
2 Right-justified
Default value: 0 (left-justified).
Thus, value 5 you used is not a valid Quadding value at all!
Furthermore, FreeText Quadding does not even support what you want to achieve, to align in center of rectangle, it only allows to select horizontal alignment, not vertical alignment.
For your objective, therefore, you'll have to use a custom appearance (which takes precedence if present).

How To Arrange a Desain Gameobject or UI to become Scrollable using Scroll Rect ? Unity C#

Please help me..
How to arrange gameobject or UI to become like the screenshot and make it scrollable using scroll rect ?
The UI must be arrange like screenshot.
There is a border yellow contain the UI and it must be scroollable.
and then there is a cyan border contain 5 Slot item or more and scrollable too. it can be generate automatically or manual.
And then there is a magenta border contain 3 button UI. Button 1, button 2, and button 3. Each button have different size.
Why it must be scrollable ? Because The Cyan border Contain and the Magenta border contain can be more than one. So if it not scrollable then the object wouldn't enough to show in the yellow border box.
I have try to make it for 1 day and i can't finish it.
Please help me how to finish it and how to make it step by step.
if you have any question just comment it.
Thanks A lot
I found the answer myself.
That is how it work.
Create Gameobject Name Panelprocess
In the Panelprocess add component : image, mask and scroll rect
Create child Gameobject Panelproceess Name ListProcess
In the ListProcess add component : Grid Layout Group and contain size fitter
at Grid Layout Group component set the value like below :
cell size : x ( 285 ) y ( 65 )
constraint : fixed column count
constraint count : 1
at Contain size fitter set the value like below :
horizontal fit : preffered size
vertical fit : preffered size
Create child Gameobject ListProcess Name Item1 (UI Image), Button1 (UI Image), Item2 (UI Image), Button2 (UI Image)
Create child Gameobject Item1 Name Slot1 (UI Image), Slot2 (UI Image), Slot3 (UI Image), Slot4 (UI Image), Slot5 (UI Image)
Note : Arrange the position and size of slot according to the screenshot
Do step 4 for Item2 too
Create child Gameobject BUtton1 Name Btn1, Btn2, Btn3
Note : Arrange the position and size of each btn1, btn2, btn3 accroding to
the screenshot.
Do step 5 for Button2 too
Back to gameobject Panelprocess
at Panelprocess component :
Scroll Rect (set the setting like below) :
content : (Drag the ListProcess gameobject to content)
Horizontal : unchecked
vertical : checked
MOvement Type : Elastic
Now Play it.
The Grid Layout Group now contain and object with a different size and scrollable.
Done

Relative UILabel in a game object

I created a game object which acts as a repeating item for a UIGrid which I populate dynamically. The gameobject (RowItem) has couple of UILabel whose text can change on runtime depending on the content. The content of these UILabels overlap when the text is bigger. Can anybody help me in how to make UILabel expand relative to the adjacent UILabel when the text is more/less?
You can use transform.localScale property of the UILabel's property to scale it. Just make them bigger when the text is bigger than let's say 20 characters. Try with arbitrary values.
Also when you change the scale, run a re-align method, which aligns other labels so that they don't overlap.
you can get the text length in pixel by this:
UILabel label;
float width = label.relativeSize.x * label.transform.localScale.x;
float height = label.relativeSize.y * label.transform.localScale.y;
Let's say that you want to set you max length to 100, you can do this:
if (width > 100)
{
label.localScale = new Vector3(100 / label.relativeSize.x, 100/ label.relativeSize.x, 1);
}
the second param for Vector3 is also based on relativeSize.x is not a typo, that makes sure your text will not become thin.
Hope this works.