Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Calculate the actual size of the content inside a UILabel
#import <UIKit/UIKit.h>
@interface UILabel (ContentSize)
- (CGSize)contentSize;
@end
#import "UILabel+ContentSize.h"
@implementation UILabel (ContentSize)
- (CGSize)contentSize {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = self.lineBreakMode;
paragraphStyle.alignment = self.textAlignment;
NSDictionary * attributes = @{NSFontAttributeName : self.font,
NSParagraphStyleAttributeName : paragraphStyle};
CGSize contentSize = [self.text boundingRectWithSize:self.frame.size
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil].size;
return contentSize;
}
@owenju

This comment has been minimized.

Show comment Hide comment
@owenju

owenju Apr 1, 2014

thxs

owenju commented Apr 1, 2014

thxs

@heqingbao

This comment has been minimized.

Show comment Hide comment
@heqingbao

heqingbao Oct 8, 2014

That's cool

That's cool

@syrakozz

This comment has been minimized.

Show comment Hide comment
@syrakozz

syrakozz Jan 22, 2015

+1

+1

@sqintian

This comment has been minimized.

Show comment Hide comment
@sqintian

sqintian Oct 22, 2015

fuck

fuck

@ricardosousaDev

This comment has been minimized.

Show comment Hide comment
@ricardosousaDev

ricardosousaDev Feb 25, 2016

Legend! Thanks!

Legend! Thanks!

@DepthSky

This comment has been minimized.

Show comment Hide comment
@DepthSky

DepthSky May 25, 2016

Thank you

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment