Skip to content

Instantly share code, notes, and snippets.

@Shosta
Last active December 20, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shosta/6174788 to your computer and use it in GitHub Desktop.
Save Shosta/6174788 to your computer and use it in GitHub Desktop.
Calculate a Label Size from its content and its position. It is useful when you want to adapt a UILabel size according to its x;y origin, its width and its content. It can then adapt a cell's height from a dynamic content.
//--------------------------------------------------------
//
//--------------------------------------------------------
// Project :
// File : UILabel+Size.h
// Created : $ 04/05/12 $
// Maintainer : $ Rémi LAVEDRINE $
//
// Copyright Rémi LAVEDRINE 2004-2012, All Rights Reserved
//
// This software is the confidential and proprietary
// information of Rémi LAVEDRINE.
// You shall not disclose such Confidential Information
// and shall use it only in accordance with the terms
// of the license agreement you entered into with
// Rémi LAVEDRINE.
//--------------------------------------------------------
//
// @brief
// Calculate a Label Size from its content and its position.
// It is useful when you want to adapt a UILabel size according to its x;y origin, its width and its content.
// It can then adapt a cell's height from a dynamic content.
//
#import <UIKit/UIKit.h>
//! @brief Calculate a Label Size from its content and its position.
//! @class UILabel+Size
//! @ingroup Utilities
//! @author Rémi Lavedrine
@interface UILabel (Size)
//! Adjusts Adjusts UILabel's frame to begin at defined x and y position and have a dynamic height
//! @param[in] startX - value that the frames x coord to be
//! @param[in] startY - value that the frames y coord to be
-(void)setUpMultiLineFrameWithStartXPosition:(CGFloat)startX withStartYPosition:(CGFloat)startY;
//! Adjusts UILabel's frame to begin at defined x position, to maxiumum width and height, centered veritcally based on maxHeight and height of content
//! @param[in] width : value that the frames width property will be
//! @param[in] startX : value that the frames x coord to be
//! @param[in] maxHeight : value that the frames height property will be
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)width withStartXPosition:(CGFloat)startX withHeight:(CGFloat)maxHeight;
//! Adjusts UILabel's frame to begin at defined x and y position, to maxiumum width and have a dynamic height
//! @param[in] maxWidth : value that the frames width property will be
//! @param[in] startX : value that the frames x coord to be
//! @param[in] startY : value that the frames y coord to be
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX withStartYPosition:(CGFloat)startY;
//! Adjusts UILabel's frame to begin at defined x position, to maximum width
//! @param[in] maxWidth : value that the frames width property will be
//! @param[in] startX : value that the frames x coord to be
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX;
//! Returns height value of UILabel based on content and max width
//! @param[in] width : max width of content
//! @return height of UILabel
-(CGFloat)getHeightBasedOnWidth:(CGFloat)width;
//! Returns size of UILabel based on content
//! @return Size of UILabel
-(CGSize)getSize;
@end
//
// UILabel+Size.m
// MobinilAndMe
//
// Created by Rémi LAVEDRINE on 04/05/12.
// Copyright (c) 2012 Rémi LAVEDRINE. All rights reserved.
//
#import "UILabel+Size.h"
@implementation UILabel (Size)
/**
@brief Adjusts Adjusts UILabel's frame to begin at defined x and y position and have a dynamic height
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(void)setUpMultiLineFrameWithStartXPosition:(CGFloat)startX withStartYPosition:(CGFloat)startY
{
self.lineBreakMode = UILineBreakModeWordWrap;
self.numberOfLines = 0;//instructs the label to contain any number of lines
CGSize minSize = [self getSize];
[self setFrame:CGRectMake(startX, startY, minSize.width, minSize.height)];
}
/**
@brief Adjusts UILabel's frame to begin at defined x position, to maxiumum width and height, centered veritcally based on maxHeight and height of content
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX withHeight:(CGFloat)maxHeight{
self.lineBreakMode = UILineBreakModeWordWrap;
self.numberOfLines = 0; //instructs the label to contain any number of lines
CGFloat labelHeight = [self getHeightBasedOnWidth:maxWidth];
CGFloat padding = ((maxHeight - labelHeight)/2); //center label within maxHeight box
[self setFrame:CGRectMake(startX, padding, maxWidth, labelHeight)];
}
/**
@brief Adjusts UILabel's frame to begin at defined x and y position, to maxiumum width and have a dynamic height
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX withStartYPosition:(CGFloat)startY{
self.lineBreakMode = UILineBreakModeWordWrap;
self.numberOfLines = 0;//instructs the label to contain any number of lines
CGFloat labelHeight = [self getHeightBasedOnWidth:maxWidth];
[self setFrame:CGRectMake(startX, startY, maxWidth, labelHeight)];
}
/**
@brief Adjusts UILabel's frame to begin at defined x position, to maximum width
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(void)setUpMultiLineFrameBasedOnWidth:(CGFloat)maxWidth withStartXPosition:(CGFloat)startX{
self.lineBreakMode = UILineBreakModeWordWrap;
self.numberOfLines = 0;//instructs the label to contain any number of lines
CGFloat labelHeight = [self getHeightBasedOnWidth:maxWidth];
[self setFrame:CGRectMake(startX, 0, maxWidth, labelHeight)];
}
/**
@brief Returns height value of UILabel based on content and max width
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(CGFloat)getHeightBasedOnWidth:(CGFloat)maxWidth{
CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(maxWidth, 9999) lineBreakMode:self.lineBreakMode];
return size.height;
}
/**
@brief Returns size of UILabel based on content
@author : Rémi Lavedrine
@date : 04/05/2012 (May the 4th be with you ^^)
@remarks : <#(optional)#>
*/
-(CGSize)getSize{
CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(9999, 9999) lineBreakMode:self.lineBreakMode];
return size;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment