gpt4 book ai didi

ios - 当我使用捏合手势放大和缩小 textview 字体大小时,UITextView 格式消失了

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:51 29 4
gpt4 key购买 nike

我想使用捏合手势放大和缩小 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;

}

}

最佳答案

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

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com