Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Last active December 9, 2015 21:19
Show Gist options
  • Select an option

  • Save jdriscoll/4329965 to your computer and use it in GitHub Desktop.

Select an option

Save jdriscoll/4329965 to your computer and use it in GitHub Desktop.
//
// UITextView+Dimensions.h
//
// Created by Justin Driscoll on 1/6/12.
//
#import <UIKit/UIKit.h>
@interface UITextView (Dimensions)
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font atWidth:(CGFloat)width;
@end
//
// UITextView+Dimensions.m
//
// Created by Justin Driscoll on 1/6/12.
//
#import "UITextView+Dimensions.h"
// This represents the total vertical and horizontal padding applied to the text.
// We have to subtract this from our original width and add it to the resulting
// height to get the correct dimensions.
static const CGFloat marginAdjustment = 16.0;
@implementation UITextView (Dimensions)
+ (CGFloat)heightWithText:(NSString *)text font:(UIFont *)font atWidth:(CGFloat)width
{
CGSize bounds = CGSizeMake(width - marginAdjustment, CGFLOAT_MAX);
CGSize size = [text sizeWithFont:font
constrainedToSize:bounds
lineBreakMode:NSLineBreakByWordWrapping];
return size.height + marginAdjustment;
}
@end
@erolando
Copy link
Copy Markdown

erolando commented May 8, 2014

update for ios 7?

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