I have a UILabel that is positioned on top of a UIImageView. The text can be various lengths, so it is necessary that I make the label as large as possible (The same size frame as the imageView). The problem is, I don't want the text from the label to cover the image by being directly in the center of the image. It's easy to set left alignment in a UILabel. Any clue how to set vertical alignment as well? Basically I want the text to hug the bottom left corner no matter what length it is without being cut off.
You can accomplish this with Autolayout. If you set up your constraints so that the label is fixed to the bottom left, but don't set a height constraint, Autolayout will use the label's Intrinsic Content Size. This just means that as the label gets more text / changes font / etc, the label will grow in height and will only ever be tall enough to contain all the text in the label.
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 have a UILabel whose size is determined by the distance of the view on top of it and the view at the bottom of it. The height of the UILabel will typically be different depending on the device. It seems that when Auto Layout stretches/crunches the UILabel's height, the text in it clings to the top of the UILabel as if the UILabel had a vertical alignment of text set to top. How can I force the text to be centred vertically?
While not understanding the perks of having Auto Layout stretch UILabel:s, I used an extra UIView in the place of my UILabel, and had Auto Layout stretch that one out instead. Then I put my UILabel centre of that containing UIView. No more constraints used for the UILabel. Seems to work great for now.
Click on the Label.
In the right side pane, select "Attributes Inspector".
For "Baseline" property, choose the option "Align Centers" to make the text centered vertically.
I want to have a UILabel with a button to it's right centered horizontally like of the picture.
I've implemented an "Align by Horizontal center" for the UILabel and standard space between label and button and it usually works just fine.
The problem appears when UILabel's text is very long and it goes out of bounds and the button is not even visible.
I've tried to set lines count for the UILabel to 0 and set questionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.questionLabel.frame)what makes the label to grow vertically. But it's width is very small (it's the base width from ui designer) so it looks like a column of text.
So the question is how to make UILabel to:
to fill all available horizontal space without hiding the button
autoscale vertically if there is no enough horizontal space to fit text into one horizontal line
be centered horizontally
It's missing the constraint from the trailing of the UIImageView to the trailing of your container view or margin. I used a Greater Than or Equal constraint so if the label has a short length, the image will stay close to the label - using the horizontal space constraint from the label to the image.
With that, your label - that is centered relative to your view, will be centered and will respect your trailing constraint of the UIImageView to the view using all the horizontal space that is possible to use as follow:
In the following image it's possible to see that the label reaches the maximum width it can, according to the constraints:
you could remove the width constraint and add a horizontal space constraint from the right side of your label to your image and one from the left side of your label to the left border of your view (set the constant to the minY Value of your Image to get it centred)
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.
Basically, I have this situation:
Two UILabels in a UITableViewCell. They're both constrained to the top of the cell. One is constrained to the left of the cell, and one is constrained to the right of the cell.
There exists a change that the UILabel on the left can run into the UILabel on the right. Is there anyway to truncate the text x points before it gets to the UILabel on the right?
Right now, I handle this by giving the UILabel on the left an explicit width that ensures it will truncate before reaching the UILabel, but the explicit width is not dynamic based on screen size. If there is a larger screen size, it might not need to be truncated. I'm new to iOS development and am not sure how to do this.
The best way to achieve this is in auto-layout. Make sure the label on the right is a fixed size, you can even change it programatically if you need to accommodate to screen size. But the thing is this one needs a width constraint. The label on the left does not have a width constraint, instead do a horizontal spacing in front of it to the cell border and from it's trailing end to the start of the label view on the right. It will widen and shrink to fit the size that's left for it.