Create a gist now

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;
}
@owenju

This comment has been minimized.

Show comment
Hide comment

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

+1

@sqintian

This comment has been minimized.

Show comment
Hide comment

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

Thank you

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