OStack程序员社区-中国程序员成长平台

标题: ios - 当我使用捏合手势放大和缩小 textview 字体大小时,UITextView 格式消失了 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:08
标题: ios - 当我使用捏合手势放大和缩小 textview 字体大小时,UITextView 格式消失了

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

我正在使用以下代码:

- (void)handleTextFieldFontOnAddMusicVcUIPinchGestureRecognizer *)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;

    }

}



Best Answer-推荐答案


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 actionselector(pinchGesture];
   pinchGestureRecognizer.delegate=self;
   [txtV addGestureRecognizer:pinchGestureRecognizer];
 }

 - (void)pinchGestureUIPinchGestureRecognizer *)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];
 }

关于ios - 当我使用捏合手势放大和缩小 textview 字体大小时,UITextView 格式消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35860663/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4