Zooming UITextView

Discussion in 'iOS Programming' started by titaniumdecoy, Sep 14, 2008.

  1. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #1
    I have a UITextView for which I want to allow zooming. I prepared a delegate class that implements the required methods and conforms to UIScrollViewDelegate. However when I set this class as the UITextView's delegate it does not work. What am I doing wrong?
     
  2. Ron C macrumors member

    Joined:
    Jul 18, 2008
    Location:
    Chicago-area
  3. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #3
    In MainViewController.m, I have this code in viewDidLoad. MainViewController conforms to UIScrollViewDelegate.

    Code:
    UIScrollView *scrollView = (UIScrollView *)[myTextView superview];
    [scrollView setDelegate:self];
    However, this causes my app to crash on launch:

    Code:
    2008-09-14 11:15:48.226 MyApp[3264:20b] *** -[MainView setDelegate:]: unrecognized selector sent to instance 0x4609a0
    2008-09-14 11:15:48.227 MyApp[3264:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MainView setDelegate:]: unrecognized selector sent to instance 0x4609a0'
    2008-09-14 11:15:48.228 MyApp[3264:20b] Stack: (
        <snip>
    )
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You have sent setDelegate: to an object that doesn't respond to it. Are you sure that's the correct method name? Are you sure scrollView is a UIScrollView? It seems odd that you are getting the super view of the text view. A UITextView is a UIScrollView via inheritance after all...
     
  5. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #5
    If I replace that code with [myTextView setDelegate:self]; I don't get an error but the text view is still not zoomable and I get a warning that says MainViewController does not implement UITextViewDelegate protocol.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Is self an instance of MainViewController? I assume so. So the next question is: does it implement all required methods in the protocol and declare that it implements the protocol in the header?
     
  7. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #7
    MainViewController implements UIScrollViewDelegate and related methods. However, it does not implement the UITextViewDelegate protocol.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Then it's not clear how you can set it as a delegate for a UITextView. Unlike desktop Cocoa textviews are not contained in scroll views: they are scroll views. Either implement the UITextViewDelegate protocol (blank methods), or find a way to call the method on the superclass...
     
  9. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #9
    That's what I'm trying to do. Although I'll take a shot at implementing UITextViewDelegate.
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Yes, but superview has nothing whatsoever to do with superclass. The superview is the view that contains this view.
     
  11. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #11
    Good point.

    I tried implementing UITextViewDelegate and that made no difference (all methods are optional). Not sure what to do now, I guess I'll have to keep looking.
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    OK, I've thought about this and I have a potential solution.

    Add a category to UITextView that adds a method to set the scroll delegate. This would allow you to set the scroll delegate without setting the text delegate. It's possible this might have unexpected consequences, but I would have thought it's pretty safe. Something like this:

    In file UITextView+ScrollDelegate.h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface UITextView (ScrollDelegate)
    
    -(void) setScrollDelegate:(id) scrollDelegate;
    
    @end
    
    In file UITextView+ScrollDelegate.m
    Code:
    #import "UITextView+ScrollDelegate.h"
    
    @implementation UITextView (ScrollDelegate)
    
    - (void) setScrollDelegate:(id) scrollDelegate
    {
    [super setDelegate:scrollDelegate];
    }
    
    @end
    
     
  13. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #13
    Thanks, robbieduncan. I got zooming working! I had to set the minimumZoomScale and maximumZoomScale as properties of the UITextView, rather than implementing them as delegate methods. I misread the docs.
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    I've not actually done anything with zooming myself, but I think I read somewhere it's up to you to zoom. Don't quote me on that. I can only suggest reading the documentation...
     
  15. titaniumdecoy thread starter macrumors member

    Joined:
    Oct 13, 2005
    #15
    You missed my edit. Got it working. Scrolling and zooming work well together. It turns out I didn't need to use the category you wrote for me (thanks, by the way), I could just use set the delegate. It wasn't working before because I tried to implement methods that returned the max and min zoom values in the delegate rather than setting those values of the UITextView itself. Thanks again. :)

    EDIT: There is one final problem. It turns out that when I am zoomed in such that the text goes off the edge of the screen, I can only scroll vertically and not horizontally. I have set the view to scroll horizontally in Interface Builder but apparently the view doesn't recognize that the content's size has changed.
     
  16. kc0001 macrumors newbie

    Joined:
    Apr 4, 2010
    #16
    Zoom in UITextView

    Hi, I am doing the same thing as you before. I have a UITextView and I would like to make it can scroll vertical and user can zoom in or out by multiple touch. I have try many method, but not good. Can you please help and let me know how you can do it well ? Thanks !
     

Share This Page