0

I'm trying to resize a UITextView to the size the text within it. The problem is that Im using a custom font and it the text doesnt fit within the UITextView.

NSString *textToFit = @"pretty long text";
UIFont *customFont = [UIFont fontWithName:@"Museo-100" size:15];
CGSize sizeText = [textToFit sizeWithFont:customFont constrainedToSize:CGSizeMake(textFrame.size.width, 1000)];

Where textFrame is the frame of the UITextView I want to adjust its height.

Im trying different fonts and also different files of the same font and still it never adjusts its height to the height that the text fills.

I've been searching and I dont find a solution. I've tried a workaround using a UILabel and the method textRectForBounds, but still no success, something on this lines.

UILabel *auxLabel = [[UILabel alloc]init];
auxLabel.numberOfLines = 0;
auxLabel.font = [UIFont fontWithName:@"Museo-100" size:15];
auxLabel.text = //THE TEXT I WANT TO FIT IN
CGRect textSize = CGRectMake(0.0, 0.0, textDescription.frame.size.width, FLT_MAX);
CGRect frame = [auxLabel textRectForBounds:textSize limitedToNumberOfLines:0];
Cœur
  • 37,241
  • 25
  • 195
  • 267
subharb
  • 3,374
  • 8
  • 41
  • 72
  • possible duplicate of [iOS7 UITextView contentsize.height alternative](http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative) – vokilam Feb 15 '14 at 19:42

1 Answers1

0

I think

UIView : sizeToFit

Should solve your problem.

sizeToFit Resizes and moves the receiver view so it just encloses its subviews.

Discussion: Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/sizeToFit

Avt
  • 16,927
  • 4
  • 52
  • 72