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 commented Apr 1, 2014

thxs

That's cool

+1

fuck

Legend! Thanks!

Thank you

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