Gopinath
Gopinath

Reputation: 5442

How to change the UIView's Y axis dynamically based on UITextView's Content size

I have an UITextView as subview of UIView. I have placed the UIView in bottom of the screen. The view controller have UITabViewController. I gave the frame size to UIView like below,

toolBarView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 330, 320, 40)];
[self.view addSubview: toolBarView1];
TextView1 = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
textViewImageView1 = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 25)];
[TextView1 addSubview: textViewImageView1];
[toolBarView1 addSubview: TextView1];

I want to increase the UITextView height based on the text inside of UITextView. I have used these below code,

    CGRect frame = messageTextView1.frame;
    frame.size.height = messageTextView1.contentSize.height;    

    NSLog(@"TextViewHeight With Texts : %f", frame.size.height);
    int height = frame.size.height;
    NSLog(@"TextViewHeight With Texts : %d", height);

    height = height + 10;
    NSLog(@"Height Plus 10 : %d", height);
    height = 370 - height;
    NSLog(@"ToolBarView1 Y axis : %d", height);


    messageTextView1.frame = frame;
    textViewImageView1.frame = CGRectMake(0, 0, messageTextView1.frame.size.width, messageTextView1.frame.size.height);
    toolBarView1.frame = CGRectMake(0, height, 320, messageTextView1.frame.size.height+10);

I want to place the UIView and UITextView bottom of the screen itself but i want to increase the height of the UIView and UITextview and Y Axis of the UIView based on the UITextview height. Can anyone please help me to do this? Thanks in advance.

Upvotes: 0

Views: 1522

Answers (1)

touti
touti

Reputation: 1164

you can get a preview of the uitextview height like this

    CGFloat constrainedSize = 252.0f;//your uitextview widght
    UIFont * myFont = [UIFont fontWithName:@"Arial" size:19];//your uitextview font
    CGSize textSize = [dis.text sizeWithFont: myFont
                          constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
                              lineBreakMode:UILineBreakModeWordWrap];
    CGRect textViewFrame = CGRectMake (0, 0, textSize.width, textSize.height);

then you can get your uitextview height from textViewFrame

Upvotes: 0

Related Questions