Resizing UITextView based on its content
Posted: October 8th, 2010 | Author: admin | Filed under: Uncategorized | No Comments »Just a quick piece of code illustrating how to resize height and width of a UITextView based on its content.
It’s important to add approximately 30 pixels to the final height and 15 pixels to the final width because sizeWithFont function does not take into account the small horizontal and vertical margins within a UITextView.
Enjoy!
-
CGSize sizeForTextBox(NSString * text, NSString *fontName, CGFloat fontSize, CGFloat max_width, CGFloat max_height)
-
{
-
UIFont *helvFont = [UIFont fontWithName:fontName size:fontSize];
-
CGSize maxTextBoxSize = CGSizeMake(max_width,max_height);
-
CGSize textBoxSize = [text sizeWithFont:helvFont
-
constrainedToSize:maxTextBoxSize
-
lineBreakMode:UILineBreakModeWordWrap ];
-
textBoxSize.height = textBoxSize.height+30;
-
textBoxSize.width = textBoxSize.width+15;
-
return textBoxSize;
-
}