I have a UILabel inside a UIScrollView and the contentSize of the UIScrollView is determined by the amount of text in the UILabel. I have managed to define a minimum height for the contentView inside the UIScrollView by making the height, width and aspect ratio constraints optional.
This is how the hierarchy of the UIScrollView looks like -
UIScrollView
contentView
UILabel
In some cases, even when the text is huge and makes the label expand beyond it's frame, I wish to have the frame of the UILabel stick to it's square frame with the extra text getting truncated. I am using autolayout so I believe this won't be possible by merely setting the frame of the UILabel and it has something to do with changing the priorities of the constraints.
Constraints for the UILabel -
The UILabel has its leading, trailing, top and bottom connected to the contentView.
Constraints for the UISCrollView-
Required
Trailing, Leading, Top and Bottom to the superview i.e. the UIScrollView
Optional
Equal height and width to the superview (UIScrollView) and aspect ratio = 1:1
EDIT
And I need to do it in a way that when I set the lineBreakMode of the UILabel to NSLineBreakByTruncatingTail, the text automatically gets truncated and textLabel.text only returns the visible text.
Ok, so if I understand everything correctly, you want to have an expandable label that either shows its entire text, or has a square form. This should as easy as enabling/disabling a constraint such that label.height <= label.width (or ==, depending on what you want it to look like if there is less text then would fit a square label). You can create this constraint in interface builder, add an outlet for it and then simply do:
self.labelSquareConstraint.active = YES/NO;
As to the question in your edit : there is no easy way to get the visible part of the text from a label, but there are some threads on SO about this, for example :
Get truncated text from UILabel
UILabel visible part of text
Calculate the range of visible text in UILabel
Related
I got a UIView whose child is a UILabel. I then aligned the left, top, right, and bottom of the UILabel to that of its superview (the UIView) using constraints.
But what I'm getting is a UILabel that matches the height of its superview but not its intrinsic height (smaller or larger than its text content depending on the size of the UIView). What I expect is for the UIView to resize itself to fit exactly the height of the UILabel.
So how do I do this using only the interface builder?
In order to make the UILabel keep its size and force the outer view to resize you can update the values for Content Hugging Priority to 1000 (a.k.a. Required). This can be done in the measurements panel when selecting the label in Interface Builder.
I think they are set as a default to 750.
This should (if there is nothing else causing the change) make the label take its intrinsic content size and force its superview to conform to that size also.
Align top, trailing, bottom and leading of UILabel with UIView (add constraints).
Remove height and bottom constraints from UIView if any.
If the content is large, you probably need to wrap UILabel into a UIScrollView in place of UIView. and add a bottom constraint to UIScrollView
Content Hugging Priority of UILabel (251) is more than that of UIView (250) by default. Verify this
It's super easy using auto-layout. Just, follow these steps
1 - Drag a UIView and align it vertically and horizontallyin centre.
2 - Now drag UILabel into the UIView and align the label also horizontally and vertically in centre.Now, the IB aligns the label at the centre WRT to the superview and not the UIView.So, change that in the size-inspector section.
3 - Once you have done that and all red lines are removed, select both the label and the UIView together using the command key.
4 - Now, give them constraints as follows
leading = 0, trailing = 0 and select the equal width and equal height.
There you are done.If you wanna test that the UIView size is respective to the label's content, try increasing the label's font to a bigger size and you will see that the size of the view will be the same as the size of the label.
There , you are done :-D. Hope, this was helpful.
If someone is experiencing this issue, while:
Having 2 labels inside the UIView
Doing everything what was suggested here
In my case, I set Vertical Content Hugging Priority of BOTH UILabels to 1000. Then one of the labels had a different font. (ie. one label was supposed to be smaller than the other one)
The height was being ambiguous, because both labels were trying to force their height on the superview. Once I lowered the priority on the smaller label, everything worked fine.
I have one UILabel and One UIView contains other subviews side by side in Storyboard. The UIView should butt up against the right edge of the UILabel(trailing constraint of 1), but I also need the UIlabel (the one on the left) to set it's width equal to it's content size unless it hits a max width. Visually:
|Label text| |UIViewWithSubviews|
And I need the following constraints:
1) Label should resize width wise unless it hits a max size and height is also dynamic that means when text reaches the maximum width then the word wrap will break the sentence to another line. and UIview will become vertical to UIlabel.
2) UIView should always be butted up against the right edge of label one in vertical center.
How do I set this up in Storyboard?
Thanks In advance.
Is this what you need?
Here is how i did in Interface Builder.
1) Set a width constraint on the UILabel that is "Less Than Or Equal" to the max width you want.
2) Set the number of lines for the UILabel in the storyboard to be 0. This enables the wrapping you are looking for. Don't set any height constraint at all.
3) Set up constraint to center the UIView vertically with the UILabel.
4) Your other existing constraints sound fine. Just make sure that the UILabel has a clear x and y position, for example by anchoring to the leading and top edges of the superview. And make sure the UIView similarly has sufficient constraints as needed in addition to the vertical centering and the leading edge alignment with the UILabel.
i have UILabel (red colored) on top and UIScrollView (blue colored) at the bottom of it. I have set all constraints for UILabel and at the same time I have set UIScrollView Top Space To: UILabel = 10.
Now when my app is loaded everything looks good. But, i am dynamically changing height of my UILabel depending on its text and logically when height of UILabel increases UIScrollView should automatically go down to keep constraint. But this does not happen. ScrollView stays as it is and overlaps with increased UILabel.
Here is what you should do.
With all of your current settings,
Don't use height constraint for label,
set number of line to 0 to label
You don't need to add any calculation of height of label, it will adjust it's height according to text
PS: if you want to have some height constraint to set minimum height of label, you can use "Greater or equal to instead of equal"
If I put a UILabel in a view and specify a vertical space constraint and an horizontal space constraint, it's fine.
If I do the same with my custom UIView, IB will complain because it'll lack an height and a width constraints.
I guess it comes from the fact that UILabel can expand both vertically and horizontally. But so does my custom UIView, so how can I inform that my view has such rights?
The default behaviour for a UILabel is "Fit To Content"(Editor-> Fit To Content). This means that UILabel will grow as the Content increases for a given font size. For a view to position itself, it needs X, Y, Width and Height. Width and Height are given by the text it contains. Rest two properties you have already provided. I hope that answers your question.
I have a simple UIScrollView that I'm adding subviews to. The problem is that I have 2 identical UILabels with symmetrical constraints, yet the UILabel on the right is not stretching to the edge of the superview (the red indicates where I want it to be, equal to the width in front of the left label) and instead seems to be resizing to only fit the width of the text all while following the other constraints placed on it properly.
Below are the associated constraints for the 3 subviews in the first screenshot. As you can see, the constraints are the same for each UILabel only flipped. It's the right label's "Trailing Space to: Superview (Equals 15)" that isn't being recognized.
Other relevant information that might be applicable are as follows:
My UIScrollView is pinned to the superview with trailing space and
leading space equal to 0. Here is the view hierarchy:
The left UILabel contains the static text "Request" but the right label's text is generated dynamically via the view controller.
I have IBOutlets for both labels. The only code I use on the outlets is this:
// Left UILabel
requestLabel.layer.cornerRadius = 10
requestLabel.clipsToBounds = true
// Right UILabel
swapTypeLabel.layer.cornerRadius = 10
swapTypeLabel.clipsToBounds = true
I'm no expert in auto-layout but would someone be able to tell me what is preventing the right UILabel from following the Trailing Space to Superview constraint and instead resizing to fit the text, and how I can go about fixing it? Thank you.