Created
February 17, 2013 21:47
-
-
Save clooth/4973661 to your computer and use it in GitHub Desktop.
Dynamic three row label framing and display
This file contains hidden or 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
// Available coordinates for article text display | |
CGFloat textDisplayStartX = self.photoView.frame.origin.x + self.photoView.frame.size.width + 10.0; | |
CGFloat textDisplayWidth = self.frame.size.width - textDisplayStartX - 5.0; | |
// Line heights | |
CGFloat titleLabelLineHeight = self.articleTitleLabel.font.lineHeight; | |
CGFloat detailLabelLineHeight = self.articleDetailsLabel.font.lineHeight; | |
CGFloat previewLabelLineHeight = self.articlePreviewLabel.font.lineHeight; | |
// Calculate title label size to determine whether to show the other labels or not | |
CGSize expectedTitleLabelSize = [self.articleTitleLabel.text sizeWithFont:self.articleTitleLabel.font constrainedToSize:CGSizeMake(textDisplayWidth, self.frame.size.height) lineBreakMode:NSLineBreakByWordWrapping]; | |
NSUInteger expectedTitleLabelLines = floor(expectedTitleLabelSize.height / titleLabelLineHeight); | |
// | |
// Decide on the article information frames | |
// | |
CGRect articleTitleFrame, articleDetailsFrame, articlePreviewFrame; | |
CGFloat articleTitleOriginY, articleDetailsOriginY, articlePreviewOriginY; | |
articleTitleFrame = CGRectZero; | |
articleDetailsFrame = CGRectZero; | |
articlePreviewFrame = CGRectZero; | |
// No space for other things | |
// Use full frame size for vertical alignment | |
if (expectedTitleLabelLines == 3) | |
{ | |
// Fully centered vertically with others hidden | |
articleTitleFrame = CGRectMake(textDisplayStartX, 0.0, textDisplayWidth, self.frame.size.height); | |
} | |
// Title is one or two lines: | |
// Deduct details row line height from frame height | |
// Deduct title row line height from frame height | |
// Maybe deduct preview row line height from frame height | |
// Split remaining space into two | |
// Make that origin.y | |
else | |
{ | |
CGFloat finalFrameHeight = self.frame.size.height; | |
finalFrameHeight = finalFrameHeight - expectedTitleLabelSize.height; | |
finalFrameHeight = finalFrameHeight - detailLabelLineHeight; | |
// Include content preview line height (and some padding) | |
if (expectedTitleLabelLines == 1) { | |
finalFrameHeight = finalFrameHeight - previewLabelLineHeight; | |
} | |
// Half of remaining space to be used for title's top origin | |
articleTitleOriginY = finalFrameHeight / 2; | |
// Details label to be a little below the title | |
articleDetailsOriginY = articleTitleOriginY + expectedTitleLabelSize.height; | |
// Title frame | |
articleTitleFrame = CGRectMake(textDisplayStartX, articleTitleOriginY, textDisplayWidth, expectedTitleLabelSize.height); | |
// Details frame | |
articleDetailsFrame = CGRectMake(textDisplayStartX, articleDetailsOriginY, textDisplayWidth, detailLabelLineHeight); | |
// Preview frame | |
if (expectedTitleLabelLines == 1) { | |
articlePreviewOriginY = articleDetailsOriginY + detailLabelLineHeight; | |
articlePreviewFrame = CGRectMake(textDisplayStartX, articlePreviewOriginY, textDisplayWidth, previewLabelLineHeight); | |
} | |
} | |
[self.articleTitleLabel setFrame:articleTitleFrame]; | |
[self.articleDetailsLabel setFrame:articleDetailsFrame]; | |
[self.articlePreviewLabel setFrame:articlePreviewFrame]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment