2

I want to zoom in and out UITextView font size using pinch gesture.
And I am applying styles like Bold, Italic, Underline etc on UITextView.
But when i zoom in and out, formatting was gone.
How can i solve this?

I am Using following code:

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


    if (pinchGestRecognizer.state == UIGestureRecognizerStateEnded
        || pinchGestRecognizer.state == UIGestureRecognizerStateChanged) {

        CGFloat currentFontSize = self.myTextField.font.pointSize;
        CGFloat newScale = currentFontSize * pinchGestRecognizer.scale;


        if (newScale < 20.0) {
            newScale = 20.0;
        }
        if (newScale > 60.0) {
            newScale = 60.0;
        }

         self.myTextField.font = [UIFont fontWithName:self.myTextField.font.fontName size:newScale];
        pinchGestRecognizer.scale = 1;

    }

}
S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
Monika Patel
  • 2,287
  • 3
  • 20
  • 45

1 Answers1

1

Objective C

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
{
   UITextView * txtV;
   UIPinchGestureRecognizer *pinchGestureRecognizer;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
   [super viewDidLoad];

   txtV=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, 280, 300)];
   txtV.text = @"Jai Maharashtra";
   txtV.userInteractionEnabled=YES;
   txtV.font= [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
   [self.view addSubview:txtV];

   pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
   pinchGestureRecognizer.delegate=self;
   [txtV addGestureRecognizer:pinchGestureRecognizer];
 }

 - (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
 {
    NSLog(@"*** Pinch: Scale: %f Velocity: %f", gestureRecognizer.scale, gestureRecognizer.velocity);

    UIFont *font = txtV.font;
    CGFloat pointSize = font.pointSize;
    NSString *fontName = font.fontName;

    pointSize = ((gestureRecognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;

    if (pointSize < 13) pointSize = 13;
    if (pointSize > 42) pointSize = 42;

    txtV.font = [UIFont fontWithName:fontName size:pointSize];
 }
Shrikant Tanwade
  • 1,391
  • 12
  • 21
  • UITextView formatting is not gone by this code and UITextView also zoom in and zoom out and i add font like [UIFont italicSystemFontOfSize:[UIFont systemFontSize]]; – Shrikant Tanwade Mar 08 '16 at 07:51
  • I do not want to zoom UITextView or UIScrollview. I just want to zoom font of UITextview.@Shrhrikant Tanwade – Monika Patel Mar 08 '16 at 08:20