Skip to content

Instantly share code, notes, and snippets.

@maxhuk
Created April 8, 2013 11:37
Show Gist options
  • Save maxhuk/5336182 to your computer and use it in GitHub Desktop.
Save maxhuk/5336182 to your computer and use it in GitHub Desktop.
A simple way to adjust the size of an UILabel to fit its contents by changing height only. Supports attributed strings.
//
// UILabel+SizeToFit.h
//
// Created by Maksym Huk on 4/4/13.
// Copyright (c) 2013 Maksym Huk. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UILabel (SizeToFit)
- (void)sizeToFitByChangingHeight;
@end
//
// UILabel+SizeToFit.m
//
// Created by Maksym Huk on 4/4/13.
// Copyright (c) 2013 Maksym Huk. All rights reserved.
//
#import "UILabel+SizeToFit.h"
@implementation UILabel (SizeToFit)
- (void)sizeToFitByChangingHeight
{
CGRect frame = self.frame;
CGSize size = [self sizeThatFits:CGSizeMake(frame.size.width, CGFLOAT_MAX)];
frame.size.height = size.height;
self.frame = frame;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment