Autoadjusting text view?

Discussion in 'iOS Programming' started by moonman239, Oct 23, 2012.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I want a text view that'll show up correctly on any device, in any orientation. Currently, I have a text view that is on the top of the view controller, but the top part is cut off when the device is in landscape mode.
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2
    Correctly, on any device, in any orientation? I don't think there is a magic way to do that. iPad screens are much bigger than iPhone screens, so a text view that fits perfectly on the iPad won't fit at all on the iPhone.

    You could set up the struts and springs to keep the top of your text view pinned to the top of the screen, and the left and right edges pinned to the left and right edges of the screen, with the interior resized automatically. The problem is the height of the view. You could write code in your view controller's viewDidLayoutSubviews method that would adjust the height of the text field once it gets resized for the target device and orientation. That code might look like this:


    Code:
    - (void)viewDidLayoutSubviews
    {
      CGSize textViewSize = bannerTextView.bounds.size;
      UIFont *bannerTextFont = [bannerTextView font];
      
    	textViewSize = [bannerTextView.text sizeWithFont: bannerTextFont constrainedToSize: textViewSize];
    
      if (textViewSize.height != bannerTextView.bounds.size.height)
      {
        CGRect bannerFrame = bannerTextView.frame;
        bannerFrame.size.height = textViewSize.height;
        bannerTextView.frame = bannerFrame;
      }
    }
    
    (Written to assume that the text view is called "bannerTextView".)

    There is no guarantee that the text will fit in the text view. You would need to add error-checking code to handle a text view that won't fit in the form.
     

Share This Page