1

I have a custom tableview cell current, and I have a UILabel that is being filled with text that is multiple lines long. However, I don't want to change the size of the custom cell, so I want the UILabel to a adapt to the given size. Here is my problem though:

  1. On first load of the table, the UILabel will not reflect the proper height, and won't show the word wrap for the first 3 or 4 cells

  2. Once I start scrolling, the new cells reflect the proper height, and scrolling back causes the old cells to reflect the proper height.

  3. If I keep scrolling back and forth, the labels shrink their width and become squished.

I am using auto layout too. Here is the code for the label as it is formatted:

private func assignBusAndRouteTextForIndex(card: ETACard, index: Int) {
        card.busNumberLabel.text = jsonValueForIndexAndSubscript(index, string: "rd")

        var route = jsonValueForIndexAndSubscript(index, string: "fd")
        route = route.stringByReplacingOccurrencesOfString("&", withString: "&")
        print(route)

        card.routeLabel.text = route
        card.routeLabel.adjustsFontSizeToFitWidth = true
        card.routeLabel.sizeToFit()
    }

    private func jsonValueForIndexAndSubscript(index: Int, string: String) -> String {
        return self.items.arrayValue[index][string].description;
    }

And here are my auto layout constraints: AutoLayout constraints of UILabel

And finally, my custom tableview cell class looks like this:

import Foundation
import UIKit

//Dependancies
import AnimationsFramework

class ETACard: UITableViewCell {
    @IBOutlet weak var card: UIView!
    @IBOutlet weak var busNumberLabel: UILabel!   // Contains 'Bus:'
    @IBOutlet weak var routeLabel: UILabel! // Contains 'Via:'
    @IBOutlet weak var circleView: UIView!  // Contains the timing circle
    @IBOutlet weak var timeLabel: UILabel!  // Contains the arrival time

    override func layoutSubviews() {
        cardSetup()
        self.addSubview(card)
    }

    func cardSetup() {
        self.card.alpha = 1
        self.card.layer.masksToBounds = false
        self.card.layer.cornerRadius = 1
        self.card.layer.shadowOffset = CGSizeMake(-0.2, 0.2)
        self.card.layer.shadowRadius = 1

        let path = UIBezierPath(rect: self.card.bounds)
        self.card.layer.shadowPath = path.CGPath

        self.card.layer.shadowOpacity = 0.2
    }

    func renderCircleForBusTime(busTime: Int) {
        ShapeRenderer.renderCircleForBusTime(circleView, busTime: busTime)
    }

    func removeCircleFromCard(view: UIView) {
        ShapeRenderer.removeRenderedCircle(view);
    }
}

EDIT:

Here are some progressive screenshots of what I am talking about. Between each screenshot is a scroll to the bottom of the list, then back up. No Scrolls

One scroll

Two scrolls

And so on...

Please note how the text progressively shrinks and starts to get cut.

David
  • 402
  • 2
  • 9
  • 22

1 Answers1

1

After much searching I found this thread on the topic: UILabel sizeToFit doesn't work with autolayout ios6

It turns out that with AutoLayout, sizeToFit is not an option. So I went looking at my auto layout settings and saw that my bottom was set to equal 30px when it should be less than or equal to 30px so that the space under the text box can be expanded. Auto Layout should handle it as long as your sides have constraints or you have a width constraint, and you make your height <= some size.

David
  • 402
  • 2
  • 9
  • 22