0

I have a custom view where I place a UIImageView and a UILabel. Label's text can be any size and it is dynamically set, so I have set the label's numOfLines to 0.

In addition, I am using Autolayout. The image view is an icon that I place to the right of the label, and I need the top edge of both the icon and the text to be aligned (not the top of the label). I have tried several ways to get rid of the padding between the label and its text within (sizeToFit, sizeThatFits) with no success, I need the height of the label to exactly match the height of the text to be able to align the top of the text with the icon... How could I achieve that?

AppsDev
  • 11,441
  • 20
  • 81
  • 163

2 Answers2

0

If you want to set dynamic height to label in that case first you need to check prefferedmaxwidth option in storyboard/xib for that label, then set uilabel constraints.

sequence to set constraints to label:- 1. Top 2. leading 3. trailing

This will raise warning if your current label height is different than required. If this is the case then select "Update Frames" option for this label.

now set bottom constraint.

no need to call sizetofit method. Autolayout will calculate height for you

KavyaKavita
  • 1,553
  • 7
  • 16
0

You can change the height of label by changing Label's height constraint by using following code,

CGSize size = [@"your text" boundingRectWithSize:CGSizeMake(label.bounds.size.width, 10000) options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : @"FontName"} context:nil].size;

labelHeightConstraint = size.height;
Uma Madhavi
  • 4,601
  • 5
  • 34
  • 70