The following code updates the Label in my prototype cell. However when the content takes up multiple lines, the UIImageView below the Label overlaps with it. I have uploaded pics of the storyboard and when the content is updated. How can I ensure that there is a fixed space between the last line of the Label and the UIImageView? I was under the impression that the spacing to nearest neighbor constraint would solve this but it seems to fix the spacing between the UIImageView and the first line of the Label.
contentText.numberOfLines = 0;
contentText.lineBreakMode = NSLineBreakByWordWrapping;
contentText.text = text;
[contentText sizeToFit];
You need to setup vertical spacing constraint between the label and imageView.
Select your label and imageView(the one overlapped) and then in Xcode
Select Editor->Pin->Vertical Spacing
This would ensure vertical spacing between them.Now as your label would resize according to text the imageView would not overlap instead would maintain a fixed amount of space from the label's last line.
Related
I have a UITableView,In this table i have created as custom cell with image & label.I have given label leading,trailing,top,bottom.I want to set the size of label to increase according to text which is happening.I have used UITableAutomaticDimension & EstimatedRowHeight.Now i want to set minimum height of cell equal to height of image.Now if text is very less in label then height of image is decreased.Now i want to keep height cell minimum as image height & increase it when there is more text.Please help how can i do it?
Looking at the view you shared i would suggest the following constraints.
Top of UIImageView to top of your cell.
Left of UIImageView top left of your cell.
Width and Height constraints of your UIImageView.
Bottom of UIImageView to the bottom of your cell which will be greater than or equal to.
Right of your UIImageView to Left of your UILabel.
Top of your UILabel to top of your cell.
Right of your UILabel to right of your cell.
Bottom of your UILabel to bottom of your cell.
Adding these constraints should solve your problems.
I'm having this weird issue with my constraints which causes the UILabel (Caption Label) to be a fixed height instead of dynamically changing height depending on the text.
I have a view (Vertical View) with a top constraint on the label above it. The Vertical View contains a view (called View) which I'm using as a divider that is centered from top to bottom with a width of 1. On the left of the divider is a UIImageView (Left Image View) with constraints leading, top, bottom equal to superview and trailing equal to View. I want to do the exact same thing to the UIImageView on the right of the divider but here is where my issue comes up.
If I use a fixed height as seen below, the UILabel above Vertical View dynamically changes its height like I want but this is obviously not how I want the UIImageView on the right to appear. I want it to be similar to the UIImageView on the left of the divider with equal height and width.
If I set the top constraint of the UIImageView on the right to the superview Vertical View, similar to the UIImageView on the left of the divider, the UILabel above Vertical View doesn't dynamically change height anymore. The UILabel now has a fixed height which I believe comes from the fact that UILabel has a height of >= 14.
How can I properly set the constraints so that I can have both UIImageViews next to each other with equal and height contained within the Vertical View and still have the UILabel above Vertical View dynamically change height depending on the text that I set the UILabel to?
On the RightImageView, you first need to get rid of the "Height = 50" constraint. This is what is causing it to be small.
Next, if that alone doesn't fix you, can you try setting the following constraints instead of using the superview for the constrains (instead make it mirror the LeftImageView):
Left: Leading spacing to divider view
Top: Align top edges to LeftImageView
Right: Horizontal space to superview (your vertical container view)
Bottom: Align bottom edges to LeftImageView
This should allow the views to remain the same height and width (assuming your distances between left/right edge of vertical container view are the same, and the distances between divider are the same).
Now, ensure the size constraint for width of the divider is set to 1 and not >= 1. Also, ensure the vertical container view has a Compression lower than the Label.
One final note--your screenshot shows the result that IB is showing you (with the dotted yellow box) on the LeftImageView. One you update your constraints correctly, this yellow box should go away.
Regarding the UILabel - if you want this to grow dynamically, you need to do the following:
myUILabel.numberOfLines = 0;
myUILabel.text = #"Enter large amount of text here";
[myUILabel sizeToFit];
I have made a TableView, in which I set
self.tableView.estimatedRowHeight = 120.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
In one cell, I place a view, in which I drag a label, and for that label, I set constraints to top and bottom equal to view top and bottom. I increase the font of the label to 150. The table view cell scales nicely.
In the second cell, I place a view, in which I drag a textfield, and for that textfield, I set constraints to top and bottom equal to view top and bottom. I increase the font of the textfield to 150. The table view cell does not scale nicely, but keeps as small as my estimated row height.
How is this possible? Which properties should I set additionally for the textfield?
I am using iOS 9.0 as Deployment target. Below are a screenshot of the constraints and a screenshot of the simulator how it currently looks like.
Tableview does grow with textfield. You might not be setting correct font family.
In cellForRowAtIndexPath
[cell.yourTextField setFont:[UIFont fontWithName:#"HelveticaNeue" size:120]];
The cell height increased:
Try this and let me know if it worked. I played a lot with it.
Hope this can help.
I have UILabels inside UIScrollView. My labels can expand to more than one line. i have set myLabel.numberOfLines = 0 because it can expand to 2 or 3 lines. It work fine when it is not shown inside UIScrollView. but when i put it inside UIScrollView, this label is shown as one lengthy label and not breaking to multiple lines. instead horizontal scroll is shown. I don't want to show horizontel scroll, instead i want my lables to expand to more than two lines and cover only the width that is available.
You have to set Content size of scrollview and set the frame of label... It will definitely work..
you have to set new size of the label according to line increase or decrease like this.
-(NSInteger) linesCount {
CGSize newSize = [self.lblstring sizeThatFits:CGSizeMake(self.lblstring.frame.size.width, MAXFLOAT)];
return newSize.height/self.lblstring.font.lineHeight;
}
so that you get that how many lines of label you have to do....
I read this answer in how to adjust the height of my label based on the text of the label:
Vertically align text to top within a UILabel
This is my code trying to set new Text and adjust the label at the same time.
But what I find out is the height of the label never get changed. It ways stays at the value I set in my xib file. I use tool, Spark to inspect the height of the label:
-(void) setNewText(NSString *)newText
{
self.myLabel.text = newText;
self.myLabel.numberOfLines = 0;
[self.myLabel sizeToFit];
[self setNeedsLayout];
}
Can you please tell me what's wrong with my code?
Thank you.
Updated:
I have tried #rdelmar suggestion. Set the width constraint for the label and num of line to 0. The height of the ui label does get adjusted. But there are extra spacing before and after the first/last line of the text.
Here is the screenshot I captured using Spark. The blue rectangle is the UILabel. As you can see, there are extra spacing before/after the first/last line. How can I remove those spacing?
You don't need to do anything in code to get your label to adjust its height. In the xib or storyboard, give the label constraints to set its position, and give it a fixed width constraint. Do not set its height. Set the numberOfLines to 0, and that's all you need to do. If you set the text with a string that's too long to fit on one line, the label will adjust its height automatically.