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.Subtitle
as 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!