Template09
Template09

Reputation: 164

zoom in and zoom out featured in ios

I have write this code in my apps:

- (IBAction)ZoomInFunction{
@try{
    UITextField *test  = (UITextField *)[self.view viewWithTag:indexNews];
    NSLog(@"INDEX NEWS : %d", indexNews);

    UIFont *font = test.font;
    if(test.font == [font fontWithSize:22])
        test.font = [font fontWithSize:22];
    else
        test.font = [font fontWithSize:font.pointSize+2];
}@catch (NSException *err) {
    NSLog(@"Error handler : %@", err);
}

}

- (IBAction)ZoomOutFunction{
@try {
    UITextField *test  = (UITextField *)[self.view viewWithTag:indexNews];

    UIFont *font = test.font;
    if(test.font == [font fontWithSize:14])
        test.font = [font fontWithSize:14];
    else
        test.font = [font fontWithSize:font.pointSize-2];
}@catch (NSException *err) {
    NSLog(@"Error handler : %@", err);

}

Sometimes the code run well, but sometimes its show an error says like this.

Error handler : -[UIView font]: unrecognized selector sent to instance 0xac70780

Upvotes: 2

Views: 482

Answers (3)

chandan
chandan

Reputation: 2453

I have apply it and I got the proper solution for it. This is due to the this following line of code

UITextField *test  = (UITextField *)[self.view viewWithTag:indexNews];

Some time when you fetching UITextView from self.view via viewWithTag:indexNews then this indexNews tag value already assign to other UIView so this line of code fetch a UIView instead of UITextField. So it is not able to call Font method via UIView so it returns error. I have a better solution for it

In your .h file implement an outlet for UITextView

@property (weak, nonatomic) IBOutlet UITextView *fontTextView;

In .m file

@synthesize fontTextView;

- (IBAction)ZoomInFunction{
    @try{

        UIFont *font = fontTextView.font;
        if(fontTextView.font == [font fontWithSize:22])
            fontTextView.font = [font fontWithSize:22];
        else
            fontTextView.font = [font fontWithSize:font.pointSize+2];
    }@catch (NSException *err) {
        NSLog(@"Error handler : %@", err);
    }

}

- (IBAction)ZoomOutFunction{
    @try {

        UIFont *font = fontTextView.font;
        if(fontTextView.font == [font fontWithSize:14])
            fontTextView.font = [font fontWithSize:14];
        else
            fontTextView.font = [font fontWithSize:font.pointSize-2];
    }@catch (NSException *err) {
        NSLog(@"Error handler : %@", err);

    }
}

I hope It works fine for your code. Thanks

Upvotes: 0

chandan
chandan

Reputation: 2453

Scaling can be possible on UITextView. UITextView that is expanding dynamically while typing the text, and scaling as the user pinches the screen(Similar behaviour can be found in TinyPost).

Apply this in viewDidLoad

    UITextView *test  = (UITextView *)[self.view viewWithTag:indexNews];

    UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTextView:)];

    [test addGestureRecognizer:gestureRecognizer];

and apply zoomIn and out on UITextView like this

- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{

     CGFloat scale = pinchGestRecognizer.scale;

    createTextView.font = [UIFont fontWithName:createTextView.font.fontName size:createTextView.font.pointSize*scale];

    [self textViewDidChange:createTextView];       
}

- (void)textViewDidChange:(UITextView *)textView{

     CGSize textSize = textView.contentSize;

     textView.frame = CGRectMake(CGRectGetMinX(textView.frame), CGRectGetMinY(textView.frame), textSize.width, textSize.height); //update the size of the textView  
 }

I hope it works for you.

Upvotes: 1

Jitendra
Jitendra

Reputation: 5081

Read this Apple Article carefully you will Understand the zoom in and zoom out functionality in IOS.

Here is the code in action:

       UITextView *textView = [UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeFontSize:)];
    [textView addGestureRecognizer:gestureRecognizer];

- (void)changeFontSize:(UIPinchGestureRecognizer *)gestureRecognizer 
 {
    UITextView *textView = (UITextView *)gestureRecognizer.view;
    float yourFontSize = gestureRecognizer.scale * FONT_SIZE;
    textView.font = [UIFont systemFontOfSize:yourFontSize];
 }

http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

Upvotes: 1

Related Questions