PDA

View Full Version : Zooming UITextView




titaniumdecoy
Sep 14, 2008, 12:48 PM
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?



Ron C
Sep 14, 2008, 12:58 PM
post some code...

titaniumdecoy
Sep 14, 2008, 01:17 PM
In MainViewController.m, I have this code in viewDidLoad. MainViewController conforms to UIScrollViewDelegate.

UIScrollView *scrollView = (UIScrollView *)[myTextView superview];
[scrollView setDelegate:self];

However, this causes my app to crash on launch:

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>
)

robbieduncan
Sep 14, 2008, 01:32 PM
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...

titaniumdecoy
Sep 14, 2008, 01:40 PM
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.

robbieduncan
Sep 14, 2008, 01:42 PM
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.

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?

titaniumdecoy
Sep 14, 2008, 01:46 PM
MainViewController implements UIScrollViewDelegate and related methods. However, it does not implement the UITextViewDelegate protocol.

robbieduncan
Sep 14, 2008, 01:49 PM
MainViewController implements UIScrollViewDelegate and related methods. However, it does not implement the UITextViewDelegate protocol.

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...

titaniumdecoy
Sep 14, 2008, 01:52 PM
or find a way to call the method on the superclass...

That's what I'm trying to do. Although I'll take a shot at implementing UITextViewDelegate.

robbieduncan
Sep 14, 2008, 01:53 PM
That's what I'm trying to do. Although I'll take a shot at implementing UITextViewDelegate.

Yes, but superview has nothing whatsoever to do with superclass. The superview is the view that contains this view.

titaniumdecoy
Sep 14, 2008, 02:04 PM
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.

robbieduncan
Sep 14, 2008, 02:07 PM
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

#import <UIKit/UIKit.h>

@interface UITextView (ScrollDelegate)

-(void) setScrollDelegate:(id) scrollDelegate;

@end


In file UITextView+ScrollDelegate.m

#import "UITextView+ScrollDelegate.h"

@implementation UITextView (ScrollDelegate)

- (void) setScrollDelegate:(id) scrollDelegate
{
[super setDelegate:scrollDelegate];
}

@end

titaniumdecoy
Sep 14, 2008, 02:48 PM
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.

robbieduncan
Sep 14, 2008, 02:50 PM
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...

titaniumdecoy
Sep 14, 2008, 02:57 PM
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.

kc0001
Apr 4, 2010, 07:27 PM
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.

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 !