iOS:通过捏缩放UITextView?

时间:2012-12-02 12:53:47

标签: iphone ios uitextview uigesturerecognizer uipinchgesturerecognizer

我有兴趣创建在键入文本时动态扩展的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  
    }

您怎么看?

5 个答案:

答案 0 :(得分:11)

尝试:

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

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

    [self textViewDidChange:createTextView];       
}

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

答案 1 :(得分: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;
}

答案 2 :(得分:2)

首先在UIPinchGestureRecognizer方法中添加viewDidLoad

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

然后将此方法放在viewController中以缩放textField字体:

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

    }    
}

答案 3 :(得分:0)

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

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

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

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

  • 检索当前属性字符串
  • 遍历字体范围并使用缩放的
  • 替换font属性
  • 使用修改后的版本替换文本视图中的文字
  • 可能您还需要在设置文本后设置文本选择

答案 4 :(得分:0)

不是最好但很容易的解决方案:

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

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