How to calculate fontsize of same height by component height? - flutter

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.

Related

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.

UILabel: Get actual font size with adjustFontSizeToFit

I have two labels in my layout.
I want the font size to adjust to fit the label bounds.
I set to true the UILabel adjustFontSizeToFit.
Moreover a need the two labels to have the same font size. So I added an auto-layout constraint to make the labels have the same size.
The issue with it is the labels do not contain the same string. Sometimes the first is longer and vice-versa. The font I use doesn't have the same width for every character which will not allow me to use a space to fill to shorter string. (This solution looks a bit ugly to me).
I tried to get the label.font.size.pointSize after the label has been displayed but the value returned is always the one I set at the beginning (before being reduced to fit).
Does anyone have an idea on how I could achieve it ?

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.

How to set automatically min width of fields by longest text in some group

I use iReport to create reports, and I would like to know if there is a way to set the width of "optically grouped fields". They should be set to the minimal size that still displays longest text. I have Static Text on left side and Text Field right of them. This Text Fields are set to the width 150 and alignment to right, but I'd like to set smaller size to wipe out white spaces.
Consider some thing like this
Name: Paul
Surname: Smither
And want automatically to
Name: Paul
Surname: Smither
etc. can be smaller then preset size but no bigger.
Is there a way?? even some component
You cannot change the element width dynamically without using Java frameworks.
The quote from JasperReports Ultimate Guide:
ELEMENT SIZE
The width and height attributes are mandatory and represent the size
of the report element measured in pixels. Other element stretching
settings may instruct the reporting engine to ignore the specified
element height. Even in this case, the attributes remain mandatory
since even when the height is calculated dynamically, the element will
not be smaller than the originally specified height.
You can read this article for better understanding the mechanism of changing the element size.
If it's genuinely just a couple of fields that come from the same row in the dataset, then you could hack something together.
Use a monospace font
Define your maximum field length with a String set to N spaces. For example:
$P{MaxLengthString} default value is 10 spaces: " "
Change your field text from $F{FirstName} to this:
$P{MaxLengthString}.substring(
$P{MaxLengthString}.length() + $F{FirstName}.length() - java.lang.Math.max($F{FirstName}.length(), $F{LastName}.length())
) + $F{FirstName}
That is... er... a bit more complex. And it only works with monospace fonts. And I can't believe I really suggested this. Don't do it. (But it ought to work.)

How can I prevent UITextfield font size from becoming smaller as you type

I have initialized a text box to have font size 16. Now I have noticed that as I type beyond the boundary of the text box, iPhone decreases the font size and then scrolls. Now If I delete everything from the text box and start typing, it starts with the small font and never becomes 16. Could you let me know how can I fix this? Can I initialize the font size at some method every time somebody starts typing?
Thanks.
In Interface Builder, if you select the text field, you can deselect the 'Adjust to Fit' checkbox in the TextField Attributes. The font should remain the same as you set it and will show an elipsis (50...) if the content exceeds the space available.
I think you have it setup so that text field adjust font size to fit the text inside.
Try not setting it or reset to what you require.
UITextField reference:
adjustsFontSizeToFitWidth
A Boolean
value indicating whether the font size
should be reduced in order to fit the
text string into the text field’s
bounding rectangle.
#property(nonatomic) BOOL adjustsFontSizeToFitWidth
Discussion
Normally, the text field’s content is
drawn with the font you specify in the
font property. If this property is set
to YES, however, and the contents in
the text property exceed the text
field’s bounding rectangle, the
receiver starts reducing the font size
until the string fits or the minimum
font size is reached. The text is
shrunk along the baseline.
The default value for this property is
NO. If you change it to YES, you
should also set an appropriate minimum
font size by modifying the
minimumFontSize property.