0

I have added a UITextView inside a UIView. UIView has some height depending upon screen sizes. UITextView can have more or less text. So I want to make the height of UITextView dynamic so if text is more then it should have more height but it should be less than the height of main view . If text is less then it should have less height.

Carmen
  • 6,177
  • 1
  • 35
  • 40
TechChain
  • 8,404
  • 29
  • 103
  • 228

4 Answers4

5

Size a UITextView to its content programmatically:

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [textView setDelegate:self];
    [textView setText:@"your long text"];
    [textView setScrollEnabled:NO];
    [self.view addSubview:textView];

    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
0

If you are using textview to display multilines of text with scrolling disabled, It is nothing more than a UILabel. I suggest you to use UILabel for the purpose.

In the storyboard, set the constraints as follows:

enter image description here

and set the line break as word wrap: enter image description here

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

Same as the first answer, but in Swift 4:

let screenSize = UIScreen.main.bounds
let textView = UITextView.init(frame: CGRect(x: 1, y: 40, width: screenSize.width, height: screenSize.height))
textView.delegate = self as? UITextViewDelegate
textView.isScrollEnabled = false
view.addSubview(textView)

let fixedWidth = textView.frame.size.width
let newSize: CGSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
var newFrame = textView.frame
newFrame.size = CGSize(width: CGFloat(fmaxf(Float(newSize.width), Float(fixedWidth))), height: newSize.height)
S. Rivers
  • 1
  • 1
-1

Try with this

-(void)dynamicTextSize{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(200, 300, 200, 30)];
textView.center = self.view.center;
[self.view addSubview:textView];

NSString *string  = @"This is pour text.a hjajksdkja kajhdsjk akjdhs jakhd skjahs ajkdhsjkad hskja akjdhs ajkhdjskar";
textView.text = string;

//UIFont *font = [UIFont fontWithName:@"Arial" size:16.0f];
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:textView.font, NSFontAttributeName, nil];

textView.backgroundColor = [UIColor lightGrayColor];

CGRect frame  = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width, 10000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];

CGRect mFrame = textView.frame;
mFrame.size.width = frame.size.width;
mFrame.size.height = frame.size.height;
textView.frame = mFrame;

//NSLog(@"frame2:%@",NSStringFromCGRect(textView.frame));
}
Jamil
  • 2,977
  • 1
  • 13
  • 23