Instantly share code, notes, and snippets.

Embed
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;
}
@juwencheng

This comment has been minimized.

Show comment
Hide comment
@juwencheng

juwencheng commented Apr 1, 2014

thxs

@heqingbao

This comment has been minimized.

Show comment
Hide comment
@heqingbao

heqingbao Oct 8, 2014

That's cool

heqingbao commented Oct 8, 2014

That's cool

@syrakozz

This comment has been minimized.

Show comment
Hide comment
@syrakozz

syrakozz commented Jan 22, 2015

+1

@sqintian

This comment has been minimized.

Show comment
Hide comment
@sqintian

sqintian commented Oct 22, 2015

fuck

@ricardosousaDev

This comment has been minimized.

Show comment
Hide comment
@ricardosousaDev

ricardosousaDev Feb 25, 2016

Legend! Thanks!

ricardosousaDev commented Feb 25, 2016

Legend! Thanks!

@DepthSky

This comment has been minimized.

Show comment
Hide comment
@DepthSky

DepthSky commented May 25, 2016

Thank you

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