0

I don't know how I can resize my cells in my UICollectionView when the UITextView within it becomes bigger while the user is writing (as I do in the following code :)

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if([text isEqualToString:@"\n"]){
        float widthLabel = 0.0f;

        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
            widthLabel = (screenWidth-6)-4-200;
        }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
            widthLabel = (screenHeight-24)-4-200;
        }
        textView.frame = CGRectMake(2, 0, widthLabel, textView.frame.size.height+10);

    }
    if(text.length == 0 && [[textView.text substringWithRange:range] isEqualToString:@"\n"]){
        float widthLabel = 0.0f;

        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
            widthLabel = (screenWidth-6)-4-200;
        }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
            widthLabel = (screenHeight-24)-4-200;
        }
        textView.frame = CGRectMake(2, 0, widthLabel, textView.frame.size.height-10);

    }
    return YES;
}

I would like to make the item cell which contains this UITextView become bigger/smaller with respect to the UITextView. I cannot change the size by stroing the new height fo the cell and use reloadData because it makes the keyboard go away and the user cannot write anymore.

Stoli
  • 69
  • 2
  • 14

1 Answers1

1

I am not sure but i think you can try to invalidate layout and force a redraw of the uicollectionview layout.

Look at this : Resize UICollectionView cells after their data has been set

Community
  • 1
  • 1
Shivam Mishra
  • 137
  • 1
  • 12
  • Yes, I saw that post earlier. I tried it but unfortunately, if I call invalidateLayout while the user is still writing, this will affect the writing ... – Stoli Apr 10 '14 at 18:17
  • Is there a way to achieve the same result (i.e : have a UICollectionView with cells containing text that make the cell fit the text size) without a UITextView ? Because, a UITextView is not supposed to grow with its content, right ? – Stoli Apr 10 '14 at 19:09
  • if you try to increase the cell size that means you are trying to alter the current layout of collection view, that requires you to call invalidateLayout and will thus dismiss the keyboard. Instead you can animate a popup, take in the user input and then redraw the whole layout depending on the content of that cell. – Shivam Mishra Apr 11 '14 at 03:18
  • Thanks , that´s what I thought. Thank you for your help – Stoli Apr 11 '14 at 05:49