Getting Adjusted fontSize after Autoshrink in Swift - swift

I am having trouble getting the font size of a label after the font autoshrinks to fit the width of the label bounds. Inside the user interface, I've set the Autoshrink capability to 'Minimum Font Size' of 9.0. The original font size is set at 17.0. Here is the method I used to get the fontSize:
labels.font.pointSize
However, even though the label has autoshrunk to say 12.0, the method still gives back 17.0.
Is there any other way to get the current 'autoshrunk' font size?

Related

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 ?

How to set label to auto height in smartface?

I am having label in my Smartface project for description field.
Is there any way to set the height of the label to auto or setting height basing on the text length.
Thanks in advance.
There is a property for that.
Check the attached picture below :
If you make the label's autosize property true, then it's width property changes automatically in order to show the text.
If this is not enough for your text, you can also use multiline property. This makes your label's height property change.
If you don't want to change label's size, then you can use "Adjust font size to fit". If you set an appropriate value for min font size, then your text font gets smaller in order to fit into the label.

iPhone: UILabel text does not fit the way i want

I have a UILabel with some text on it, what I want is if the text with the given font does not fit to the label, I want it to be first linebreaked to a second line, and if still does not fit then it should automatically adjust those 2 lines to a smaller font.
I experienced with the IB changing the settings of linebreaks and number of rows, but couldn't get what I want.
Any recommendadtions?
To my knowledge UILabel does not support auto adjusting the font when there is more than one line.
The only way I know of it to iteratively calculate a fitting font size and then to set the appropriate font manually.
Maybe the sizeWithFont: method is a solution for you:
– sizeWithFont:forWidth:lineBreakMode:
This calculates the width / height of a NSString with the appropriate font / settings

How to fit a String into a rectangle?

I have an NSString and want to fit it into a rectangle. The rectangle has a specified size, lets say width=150 and height=30. When the String is short and has only one character, it can be as high as the rectangle. More specific: It can have a big font size. But if the string has too much characters and would exceed the bounds of the rectangle, it must become smaller. More specific: It's font size must become smaller, so that it won't exceed the bounds of the rectangle. Is there a way of doing that without messing around in core graphics?
For some reason, UILabel's adjustsFontSizeToFitWidth Property has no effect. The text keeps beeing small even if there is plently of space.
I've set that to
label.adjustsFontSizeToFitWidth = YES;
but nothing happens. I hope there is another way to do that...
There are several part to UILabel that make this possible, but first you have to know if you want to truncate the string or make the font size smaller to fit in the rectangle.
For both cases you'll want to set the UILabel's numberOfLines property to 0, allowing the label to wrap as much as necessary. Then you'll want to set the frame of the UILabel to match the rectangle you're looking to fit. From there you take one of two paths:
Truncation: Set the lineBreakMode property to UILineBreakModeClip, UILineBreakModeHeadTruncation, UILineBreakModeTailTruncation, or UILineBreakModeMiddleTruncation depending on the truncation behavior you're looking for.
Resizing: Set the lineBreakMode to either UILineBreakModeWordWrap or `UILineBreakModeCharacterWrap' depending on your preference. Then you'll need to enter a loop to figure out the right font size. Start with a reasonable font size (e.g., 12) and:
Set the font property of the UILabel with a UIFont that matches that size
Call - (void) sizeToFit for the UILabel.
Check the frame for the UILabel:
If the frame will fit within the bounds you need it to, you're done
If the frame is still to big, drop the size of the font and repeat the loop
For the latter option you'll want to make sure you're not squeezing the text into oblivion, so you'll want to put a minimum size cap on the font size.
You can get more information from the UILabel and UIFont documentation.
adjustsFontSizeToFitWidth adjusts the font size down, not up.
Set the font on your UILabel to an appropriately large size, and UILabel will shrink it when necessary to fit in its bounds.
Set label font size to desired normal size.
Set label minimum font size to
smallest possible font
(minimumFontSize property)
Set adjustsFontSizeToFitWidth to
YES.
If you need multiple lines, set lineCount to 0 as the other poster noted.

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.