当我使用捏合手势放大和缩小文本视图字体大小时,UITextView 格式消失了



我想使用捏合手势放大和缩小字体大小UITextView
我正在UITextView上应用粗体、斜体、下划线等样式。
但是当我放大和缩小时,格式消失了。
我该如何解决这个问题?

我正在使用以下代码:

- (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;
    }
}

目标 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];
 }

最新更新