简体   繁体   中英

label height issue in iOS?

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

//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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM