I'm trying to adjust my label size based on screen size using swift. I'm using the dynamic layout and cannot figure it out. Essentially what I am looking to do is shrink the text/font size to fit within my frame.
You can do this all in the storyboard.
Add constraints between the leading and trailing edges of the label and its superview. (Something like |-[label]-| in the visual format.) This will adjust the width of the label as the width of its superview changes.
If you want the font size to adjust as well, you can set the Autoshrink item in the storyboard to Minimum Font Scale and set an appropriate value (0.5 is the default).
Related
I am using a UILabel in a UITableViewCell to display a title. Normally the text is not too long and therefore the font size can be large. Notice how the text fills the height of theUILabel like normal.
However, when there is a larger title, I want the UILabel height to reduce to accommodate the smaller font size and not leave a blank gap in its place. Currently, my configuration produces this effect.
I am using constraints in my storyboard and have deliberately not set a fixed height constraint. Here are the relevant constraints added:
I may have made a rookie error in my configuration as I can't remember this effect happening before, but it is giving me real headaches (and a bad looking UI).
When UILabel is set to auto-adjust font size to fit, it does NOT auto-adjust the height of itself.
You have a couple options:
set Baseline to Align Centers, and just accept that you will have top and bottom padding
use code to calculate the "scaled font size" and update the font size of the label
Remove either the Top Space or Bottom Space constraints (depending on where you want the label to be anchored).
This will cause the label to automatically use fit itself to the text size.
Try this and see, it should work according to your need:
Set Top and Bottom constraints attribute Greater Than or Equal To and add horizontal center (Alight Center Y to superview) and show in this image.
I'm using auto-layout (programmatically) and I'm facing a problem.
I have three buttons aligned side-by-side, and I'm trying to adapt their titleLabel's font size so they fit their width.
The thing is, I'd also like these three buttons to have the same font size, so everything is consistent.
But if I use this property :
self.titleLabel.adjustsFontSizeToFitWidth = YES;
Then, only one of the buttons adapts its font size.
Is it possible to set a constraint on the font size, or do you guys have an other idea?
I have a multiline UILabel inside a custom keyboard extension. I want this label to grow to fill the content up to a certain height, at which point I want it to just cut off the rest of the text.
Because the keyboard has different heights depending on the device and orientation, I can't just set a simple less than or equal height constraint.
What I tried was to constrain the bottom of the label to the top of the buttons below, with a greater than or equal constraint. This works to a certain extent, but causes the keyboard to grow in size as opposed to the label being forced to cut off its text.
How would I force the label to a max height, without directly using a height constraint on the label?
Put the label in a UIView and constrain the view's height to less than or equal.
I'm using autolayout and my UILabel doesn't seem to get bigger when the app is opened on the iphone 6 and 6+. I have the following constraints on the label:
-Vertical spacing to top layout guide
-Vertical spacing to image below label
-Center horizontally in container
I also tried setting an aspect ratio constraint to itself and then a equal widths constraint with the parent view (with multiplier ratio) so that when the width of the parent view expands so would the label but no luck.
Are there settings that I should be implementing to allow this to happen? I thought labels have intrinsic sizing so they would get larger on their own as the parent view grows.
No, they have intrinsic sizing so they would get larger as their content grows (i.e. how much text they contain). That is what intrinsic sizing means. It means that the size is intrinsic.
Also, resizing a label physically (e.g. making it taller and wider) changes the size of the label. This will be evident if your label has a distinct colored background, for instance. It has no direct effect whatever, though, on the size of its text's font.
I set inset constraints for 2 labels, and width is set to fixed value. number of lines is set to 0.
What I expect is the UILabels are resized to fit, and the parent view is also auto resized. But the output is the container remains the same size but the sub uilabels are either too high or too short. What's the problem? Thanks.
Here is the constraints
Try setting the content hugging priority to a higher value. This affects how resistant a view is to expanding past its intrinsic content size.
The image of UILabel looks like it is truncating the contents. So if you want to display the text in the next line if it is bigger then you need to use the below api.
label.numberOfLines=0;