How can I get a text width? - gtk

I want to modify a Gtk.Entry to fit its text width, how can I get the text width?. I've tried setting an average width for each character, but It don't work properly

GTK+ widgets have their rendering layout done by Pango, so first, create a Pango Layout for your widget by calling create_pango_layout with the text you want on your Gtk.Entry and then use get_size to get the dimensions.

Related

How to calculate fontsize of same height by component height?

There's a RectComponent, and a TextComponent inside. The text will change length sometimes.
So I need "text adaptation".
The fontsize :
The size of glyphs (in logical pixels) to use when painting the text.
I cannot find the conversion method between fontSize(logical pixels) and component size.
Is there some formula between "component size" and "font size"?
There is currently no way to automatically fit the text in the size of the RectangleComponent.
You can however use TextComponent.textRenderer.measureWidth and calculate whether the font size has to be smaller or larger than it currently is.
Remember that if you don't use a mono-spaced font, a string with the same amount of characters as another string might become different widths.
You can use FittedBox, check this answer out:
How to dynamically resize text in Flutter?
So, basically, FittedBox makes sure the content always stays within an area.

What's the proper way to align a strechable Text and an image to two ends in Unity UI?

I have an UI element that has a Text and an Image as its children. It has a HorizontalLayoutGroup component that enables Control Child Size and Child Force Expand.
I want the Image has a fixed size, and Text has strechable size controlled by the HorizontalLayoutGroup. So when the Image is set to inactive, the Text fills the whole space and when the Image is active, the Text shrinks a little bit in order to give space to the Image. Right now this part works good.
My second goal is to align them to both ends: the Text in the left and the Image on the right with space in between. But changing Child Alignment can't achieve this.
I tried the following solution:
Add LayoutElement both to Image and Text. On Text, enable Flexible Width and set a a value, on Image, enable Min Width and set to a value. Manually adjust the two values until it seems right.
This solution seems to work, but I don't know why. Is anyone familiar with this?What's the recommended way to do it? Thanks!
I worked it out. On the LayoutElement, treat Preferred Width or Preferred Height as Max Width or Max Height. Enable them, set the value the same with min value. One the other objects that you want to stretch, enable Flexible values. Then all worked as we want.

Setting row to minimum child height flutter

I'm trying to produce a widget like this below to be contained inside of a listview. I have managed an effect like this rather simply based on the example given on the flutter website for YouTube. However, the part I'm struggling with is that I would like the image to resize vertically depending on how large the text is.
Currently this is created with a row. If possible I'd like to set the row height somehow but only dependent on the height of the text. As some elements will have more text and some will have less I would like to have the image fill the vertical space so the height is the same as the text height.
I basically want this - https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4
but where the image height on the left matches the text height on the right
Thanks in advance!

Scale text size in FPDF

I'm using FPDF and I have a little problem:
I am printing dynamic text in a line that have a maximum width. The text has a maximum height and, when the text have more width that the line, I'm decreasing the font size of the text until it fits to the line.
It works fine, but I want to know if I could decrease only the text width, scaling it and mantaining the height that I declared at the beginning. I've read the documentation and searched for a solution but I didn't find anything related to this.
Thanks.
Finally I found an add-on to scale the text and it works great:
http://fpdf.de/downloads/addons/62/
Instead of using the Cell() function, I use the CellFitScale() function and it scales automatically only the width of the text.

GWT - how to precisely set rendered width of TextArea in pixels?

I need to render a TextArea in an exact pixel width for the special purposes of my GUI.
When I set its width to "250px", it comes out as 256px.
Is there a way to override this behavior so I can render it as 250px exactly?
When you use setWidth(), you're effectively setting the element's content width.
To get the offset width use UIObject#getOffsetWidth(). This will return the width including padding and border (but not margins).
References on W3C
Computing widths and margins
The box model