Skip to content

Instantly share code, notes, and snippets.

@wwwins
Forked from billyohgren/UILabel+ContentSize.h
Last active August 29, 2015 14:10
Show Gist options
  • Save wwwins/08186bdf3357dccf2e00 to your computer and use it in GitHub Desktop.
Save wwwins/08186bdf3357dccf2e00 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment