9

我有兴趣创建UITextView在键入文本时动态扩展的内容,并在用户捏住屏幕时进行缩放(在 TinyPost 中可以找到类似的行为)。

当您只需键入(不捏)时,textView 会很好地展开。当你只是捏(不打字)时工作正常,但是当你捏然后打字时,里面的文字会被剪掉。

这是我的代码:

UIPinchGestureRecognizer *pinchGestRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTextView:)];
        pinchGestRecognizer.delegate = self;
        [bgFrameImageView addGestureRecognizer:pinchGestRecognizer];

    - (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{
        createTextView.transform = CGAffineTransformScale(createTextView.transform, pinchGestRecognizer.scale, pinchGestRecognizer.scale);

        pinchGestRecognizer.scale = 1;        
    }

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

你怎么看?

4

5 回答 5

14

尝试:

- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{
     CGFloat scale = pinchGestRecognizer.scale;

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

    [self textViewDidChange:createTextView];       
}

它基本上会缩放字体大小,然后使用您在 textViewDidChange 中的代码重新计算内容大小。

于 2012-12-06T20:24:44.550 回答
2

详细说明上面的@Cocoanetics 答案。我在 iOS 7 上实现了属性字符串的手势处理想法,但是当字符串中的字体更改过多时,它的速度非常慢。iOS 7 中还有一个可笑的缓冲错误,在您停止捏合后,更改通知会持续触发很长时间 - 让我想起了早期版本的 PC-DOS 中愚蠢的键盘缓冲区。无论如何,我已经将代码放在下面,这对我有用——尽管它只会告诉我这是浪费时间,我需要给我的用户一些其他方式来缩放他们的字体。

- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    CGFloat scale = 0;
    NSMutableAttributedString *string;

    switch (pinchGestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            self.old_scale = 1.0;
            self.last_time = [NSDate date];
            break;

        case UIGestureRecognizerStateChanged:
            scale = pinchGestureRecognizer.scale - self.old_scale;

            if( [self.last_time timeIntervalSinceNow] < -0.2 )  {       //  updating 5 times a second is best I can do - faster than this and we get buffered changes going on for ages!
                self.last_time = [NSDate date];
                string = [self getScaledStringFrom:[self.textview.attributedText mutableCopy] withScale:1.0 + scale];
                if( string )    {
                    self.textview.attributedText = string;
                    self.old_scale = pinchGestureRecognizer.scale;
                }
            }
            break;

        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            break;

        default:
            break;
    }
}

- (NSMutableAttributedString*) getScaledStringFrom:(NSMutableAttributedString*)string withScale:(CGFloat)scale
{
    [string beginEditing];
    [string enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, string.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * scale];
            [string removeAttribute:NSFontAttributeName range:range];
            [string addAttribute:NSFontAttributeName value:newFont range:range];
        }
    }];
    [string endEditing];
    return string;
}
于 2014-06-20T20:40:46.353 回答
2

First of all add UIPinchGestureRecognizer in viewDidLoad method:

UIPinchGestureRecognizer *pinchOnTextfield = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleTextFieldFontOnAddMusicVc:)];
[self.myTextField addGestureRecognizer:pinchOnTextfield];

then put this method in your viewController to scale textField font:

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

    }    
}
于 2015-10-06T12:01:04.187 回答
0

UITextView 是 UIScrollView 的子类,因此您需要对任何滚动视图执行您所做的操作以启用缩放:

  • 设置最小和最大缩放比例属性
  • 通过滚动视图委托方法设置 viewForZooming

...缩放整个文本视图。

如果您只想缩放文本,则必须通过捏合手势识别器执行以下步骤:

  • 检索当前的属性字符串
  • 遍历字体范围并用缩放的字体替换字体属性
  • 用修改后的版本替换文本视图中的文本
  • 可能您还需要在设置文本后设置文本选择
于 2012-12-10T09:16:10.463 回答
0

不是最好但简单的解决方案是:

我不完全了解您的用例,但我认为在许多情况下,在输入文本时支持应用程序捏缩放是没有意义的。我只想设置一个标志,以防止在用户输入文本时进行缩放。

如果您的意思是输入文本,编辑文本视图,您可以在捏合手势开始时结束编辑,并在捏合手势结束后开始编辑。

于 2012-12-10T13:05:15.167 回答