0

i have added 4 views in parent view vertically.I have given them below constraints

  1. equal width to parent
  2. fix height of 70
  3. vertical margin between views of 1
  4. center x of parent view

Now the text in labels can grow it's not static.As of now text inside label was showing vertically center so i added below code to make text align to the top of label not show in center vertically of label (code from here)

//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

if i use above code with label then it gives me height of 67.201 accroding to some text but the height should be more because text is more but if i not use above code then text is placed vertically center inside label.

I want to make text to top aligned of label not center vertically.So that height of label should decrease it should not have any top,bottom padding.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Dheeraj Kumar
  • 327
  • 2
  • 6
  • 19
  • which height to log & what to do with that height – Dheeraj Kumar Mar 18 '16 at 05:40
  • You have to use attribute string. Check this answer http://stackoverflow.com/questions/36028204/calculate-height-and-number-of-lines-of-the-label/36029336#36029336 – kb920 Mar 18 '16 at 05:53

1 Answers1

0

You can't align the text to the top of the label, only reduce its overall height. You could simulate the effect using a container view with that fixed height and let the label adjust freely inside of that view.

Look at this question: Vertically align text to top within a UILabel

Community
  • 1
  • 1
Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • Okay fine but in my case height of label can incrase beased on text but i want label text aligned to top in that case also – Dheeraj Kumar Mar 18 '16 at 05:42
  • Then you could set the container view to have a height >= 70 (priority 1000) and then height = 70 (priority 750, different constraint) and make the label adjust to its contents using the `sizeToFit` method (leading, trailing and top constraints from the label to the container view to `0`). – Alejandro Iván Mar 18 '16 at 05:45
  • why to constraint for height with diffrrent priorties – Dheeraj Kumar Mar 18 '16 at 05:47
  • The highest priority will say that your container has to be 70 or more points height. The second one fixes the height to 70 with less priority. If the label goes beyond 70, Auto Layout will "break" one of those constraints (the fixed 70 points height because of the less priority). – Alejandro Iván Mar 18 '16 at 05:49
  • why can't i just first constraint – Dheeraj Kumar Mar 18 '16 at 05:56