0

I am facing an unexpected behavior with my UIlabel sizeToFit() method. Now I have tried making the numberOfLines = 0, I event called LayoutIfNeed(). But None of them works.

I even tried methods given in this question : Vertically align text to top within a UILabel

But Again None helped. I don't have to many constraints, I am just using Auto resizing pins. I even tried it with no constraints or no auto resize.

I have this label set up in TableViewCell and CollectionReusableView. Calling it in awakeFromNib() just doesnt affect.

UIcollectionReusableView code :

override func awakeFromNib() {
    super.awakeFromNib()


    label.sizeToFit()

    label.numberOfLines = 0
    label.layer.shadowOffset = CGSize(width: 0, height: 0)
    label.layer.shadowOpacity = 3
    label.layer.shadowRadius = 8

}

constrains :

enter image description here

TableViewCell Code:

override func layoutSubviews() {
    super.layoutSubviews()

// this is the UIview on which the label is put on.
    contentView.layoutIfNeeded()

}


override func awakeFromNib() {
    super.awakeFromNib()
    // This is the label has the issue
    caption.sizeToFit()

}

Constraints :

enter image description here

Also, Label in the text view is stacked with Another Label and stackview constrainst are :

enter image description here

here is the example:

enter image description here

It Just doesn't seem to work. I am totally out of idea.

Any help is greatly appreciated.

Aakash Dave
  • 866
  • 1
  • 15
  • 30
  • 2
    Post the actual code you're using so someone can try to help you. There isn't enough info here to provide any meaningful advice. – Dima Nov 09 '17 at 21:57
  • updates question. Anymore info required? – Aakash Dave Nov 09 '17 at 22:02
  • can you also post how you have created the label? are you using storyboard or creating it in code? – Nevin Jethmalani Nov 09 '17 at 22:28
  • @NevinJethmalani I have created the labels in the storyboard itself. i am just defining the properties here. Do you want me to post a screenshot of the properties? – Aakash Dave Nov 09 '17 at 22:30
  • I just want to see what constraints you have put into place. It looks like your label is getting cut off because of a trailing constraint at first glance. Is this is a tableview cell or just in a vc or in a scrollview. Can you give a little more info. – Nevin Jethmalani Nov 09 '17 at 22:51
  • @NevinJethmalani Appreciate your reply. I updated my question. Please check. – Aakash Dave Nov 09 '17 at 22:59
  • 1
    Why are you calling sizeToFit()? Are you calling sizeToFit() before you set the text in the label? Does the text vary between cells? – PhoneyDeveloper Nov 10 '17 at 01:26
  • @PhoneyDeveloper I want the labels to have text completely aligned to its left and should start from the top. The text in the labels is just in the center horizontally and vertically. And yes, the text varies in each cell. – Aakash Dave Nov 10 '17 at 09:17
  • Have you created an outlet for the label? Or are you using the default label property of the tableViewCell and adjusting the number of lines for that in your code? – Nevin Jethmalani Nov 10 '17 at 23:08
  • I also need more info from your storyboard. It is really hard to diagnose storyboard issues without all the info on constraints. This does not seem like it is a difficult issue to solve if you give all the information about how you have set up the view. – Nevin Jethmalani Nov 10 '17 at 23:11
  • @NevinJethmalani Alright What info would you need from the storyboard and the code. Let me know. I cant't post the all storyboard screenshots. Please tell me all thing. And I'll update them. And thanks for helping out promptly. – Aakash Dave Nov 11 '17 at 21:04
  • @NevinJethmalani I solved the issue. As #phoneyDeveloper mentioned. I was calling size to fit event before the text was set internally, Since I was using a async function to load text from server – Aakash Dave Nov 12 '17 at 22:12
  • @PhoneyDeveloper You are right. I sizeToFit() was being called before my text was downloaded and set. Thanks for the help. You might aswell post an answer so I can Upvote and accept it – Aakash Dave Nov 12 '17 at 22:13

1 Answers1

1

You need to call sizeToFit() after the text is added to the label, not before.

PhoneyDeveloper
  • 1,100
  • 7
  • 7