I am creating a vertical UIStackView inside a UIView and placing UILabel elements inside it.
Now, I want to change the height of Label inside the UIStackView. But the height property seems to be disabled.
Alternatively, I am able to set height constraint for the label and change it's value programatically. I want to know if there is a way to change the value in the XIB itself as my purpose is to set a static value and cut down on adding code for it.
First add UIView in UIStackView and Then Add UILabel under view. Set label's constraint. Now you can just change the UIView Frame from Size inspector, it will automatically change the label frame.
Related
Here is the screenshot of UIView with its constraints.
It is located in a tableview cell
When I use UIlabel without UIView I can get it to grow multiline. But It doesn't work with UIView which I use for padding
I tried sth like
cell.layoutIfNeeded()
cell.label.sizeToFit()
cell.label.layoutIfNeeded()
cell.view.layoutIfNeeded()
cell.view.sizeToFit()
How can I make multiline work?
Make sure you did not set any required static height for the label nor the view itself.
Make the label (which is the subview of the view) edges equal to the superview's.
Last but not least, set the values of content hugging and content compression resistance priorities to those you need.
And btw. - If all you need is the grey background that visually wraps the label by some constant, the label doesn't have to be a subview of that grey background view, just set the view's edges to the label's ones and do some layout corrections to fit your needs.
1.Set background color from attribute inspector for Label.Don't use background view for label.
2.Set UIlabel's lines property to no of lines that you want.
3.Don't give height constraints.give only top leading trailing and width.
1- Set UIlabel's lines property to no of lines that you want, you can set it to 0, then the UILabel will automatically resize
2- Don't give height constraints, give only top leading trailing and width
3-return in heightForRowAtIndexPath: UITableView.automaticDimension (Swift 4.2)
I currently have a Stack View setup with a bunch of different objects inside of it, and I'm trying to add a view to it.
The view will have a dynamic number of labels added inside of it programmatically at run-time.
When I put the view inside of the stack view, I get errors with the constraints, which get fixed by adding a height constraint.
However, I don't know the height of the view initially so I can't constrain this.
Is there any way to have a UIView inside of a StackView auto-resize based on it's content size? I thought this would be automatic, but when I don't put a height constraint, the view doesn't show up
You don't have to put a content view inside the stackview to add the elements to it , you need to hook the stackview with only ( leading , trailing , top) constraints to the superView
then add elements with
stack.addArrangedSubview(view)
the most important thing is that the elements you add must have an intrinstic content-size like label and button , and if not then add a height with
someView.heightAnchor.constraint(equalToConstant:<#setValue#>).isActive = true
with distribution set to Fill
Steps to add custom UIView with dynamic height inside UIStackView
Add UIStackView to superview in IB and add top, bottom, leading and trailing constraints.
In case IB is throwing any error for constraint, just set intrinsic size for UIStackView as PlaceHolder with some dummy size. This will fix UIStackView position in IB but this will automatically change at run time as per auto layout.
Create instance of CustomUIView as below
Bundle.main.loadNibNamed(nibName, owner: self, options: nil).first as! CustomUIView
Add UIView to UIStackView as below.
stackView.addArrangedSubview(customView)
This will add custom UIView to UIStackView and it will expand/collapse as per xib content and constraints. Just make sure your xib has proper constraints as per auto layout guidelines.
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
I have a custom table view cell with UILabel and UIImageView that designed with constraints. I want to set the UIImageView x-postion as after label end. So UIImageView x-postion will change periodically.
How I handle this situation? even I set the frame in coding which doesn't change while Autolayout.
I have a view which has on top a UITextView then a series of other views. The idea is to resize the Text View on its content size and to move up all the other views.
I have set all the constraints in storyboard and if I change the height constraint value of my TextView from there, all the view updates fine.
On runtime I can resize the Text View frame to fit it's content size, but the rest of the view doesn't change. So I'm guessing I don't have to change the frame but only the height constraint.
Any suggestion on how is it possible to do this thing on runtime?
How can I change the Height constraint value in code?
Is there any way to "link" a constraint to the controller from storyboard?
Thanks for helping
Make an IBOutlet of height contraint from nib to your .h file.
Lets name it textViewHeightConstraint
self.textViewHeightConstraint.constant = textView.contentSize.height
U need to set a textviews property Scrollable to false