2

after tirelessly trying to find a solution for calculating row height I have hit a wall. Ever since size with font was depreciated in iOS 7, I am having a difficult time calculating the approximate height on a tableview cell. Some of my cells use autolayout and it works great. The ones that don't use autolayout is where I am running into trouble. So here's my scenario. For the cells that do not use auto layout, I use UITableViewCellStyle.Subtitleas the cell type . Some of my rows use the default textlabel.text. I use the following method below and it works great!

Heres my constraint size I pass:

var contraintSize: CGSize = CGSizeMake(tableView.frame.size.width - 40 ,9999.0)

And the api I call:

func returnSingleTextLabelHeight (Font : UIFont, Text: String, constraint : CGSize) ->CGFloat {      
           textLabel.font = Font       
           textLabel.text = Text as String
           textLabel.numberOfLines = 0
           textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
           var expectSize = textLabel.sizeThatFits(constraint)   
           return expectSize.height
        }   

Now here's where I am running into trouble. When I use textLabel.text and I use detailTextLabel.text with new line characters (emphasis) in the text.

   func returnTextLabelHeightAndDetailTextLabelHeigt (Font : UIFont, Text: String,Font2 : UIFont, Text2 : String, constraint : CGSize) ->CGFloat {
        textLabel.font = Font
        textLabel.text = Text
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        var expectSize = textLabel.sizeThatFits(constraint)

        detailTextLabel.font = Font2
        detailTextLabel.text = Text2 as String
        detailTextLabel.numberOfLines = 0
        detailTextLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        var expectSize2 = detailTextLabel.sizeThatFits(constraint)
        return expectSize2.height + expectSize.height
    }

returnTextLabelHeightAndDetailTextLabelHeight works, but my problem is that it over calculates the height most of the time. I am having trouble finding something that produces similar consistent height results just like returnSingleTextLabelHeight does.

I have tried boundingRectWithSize over and over again, and I have not gotten consistent height results. This answer here brings up a good point: https://stackoverflow.com/a/25941139/4854178 When I remove the new line spacing in the text, I do get consistent height results:

I would like to have new line characters in my detailTextLabel because the formatting looks better that way. I would hate to have to create multiple cells just to display the effect I am trying to achieve. Lastly, textlabel and detailTextLabel have different font types i.e bold not bold. My question is long, so I do appreciate your time to think through and read it. Hopefully I am clear. Thanks!

Community
  • 1
  • 1
imnill
  • 59
  • 5
  • What I've done is set the text into a dummy label (not in any view) and resize it to "fit", then query the height of the label. – Hot Licks Jun 06 '15 at 18:22
  • Could you perhaps provide an example? Here's what I am thinking about doing. Like you said, I'll create a dummy label. But I am thinking about using a textLabel detailTextLabel inside of a dummy uitableviewcell, plunging in the respective fonts that way. Would I do something like cell.sizeToFit? And return the cell.frame.size.height? – imnill Jun 06 '15 at 18:43
  • I no longer have access to the code. As i recall I used a vanilla label and did `sizeToFit` on it, then checked its size. Somewhere I answered a question with example code. – Hot Licks Jun 06 '15 at 20:50

0 Answers0