Created
January 18, 2016 11:27
-
-
Save bishalg/e65625799ce395b81d4d to your computer and use it in GitHub Desktop.
Height For CollectionViewCell Label Dynamic Font scale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func heightForIndexPath(indexPath: NSIndexPath, inWidth width: CGFloat, andHeight height: CGFloat) -> CGFloat { | |
if indexPath.item % 2 != 0 { | |
return cellHeight | |
} | |
let labelLeft = UILabel(frame: CGRect(x: 0, y:0, width: width, height: height)) | |
labelLeft.font = UIFont.fontForTextStyle(UIFontTextStyleHeadline, scaleFactor: Fonts.FontScale.Small.value) | |
labelLeft.numberOfLines = 0 | |
let video = videosInCollection[indexPath.row] | |
labelLeft.text = video.description | |
labelLeft.sizeToFit() | |
var finalHeight = labelLeft.frame.size.height | |
if indexPath.item % 2 == 0 { | |
if indexPath.item >= videosInCollection.count { | |
return finalHeight | |
} | |
let labelRight = UILabel(frame: CGRect(x: 0, y:0, width: width, height: height)) | |
labelRight.font = UIFont.fontForTextStyle(UIFontTextStyleHeadline, scaleFactor: Fonts.FontScale.Same.value) | |
labelRight.numberOfLines = 0 | |
let rightIndexPath = NSIndexPath.init(forItem: indexPath.item + 1, inSection: indexPath.section) | |
let video = videosInCollection[rightIndexPath.row] | |
labelRight.text = video.description | |
labelRight.sizeToFit() | |
finalHeight = max(finalHeight, labelRight.frame.size.height) | |
} | |
cellHeight = finalHeight | |
return finalHeight | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment