使用UITextview调整大小来缩放字体大小

时间:2014-02-21 08:59:46

标签: ios iphone resize uilabel uitextview

我想创建UITextView缩放并用手指触摸旋转(平移手势)。但问题是文本字体大小未正确缩放。请帮忙。

它有效但不完美。

-(void)resizeTranslate:(UIPanGestureRecognizer *)recognizer
{

    if ([recognizer state]== UIGestureRecognizerStateBegan)
    {
        prevPoint = [recognizer locationInView:vw_txtfield.superview];
        [vw_txtfield setNeedsDisplay];
        olddistance = sqrt(pow((vw_txtfield.frame.origin.x - prevPoint.x), 2.0) + pow((vw_txtfield.frame.origin.y - prevPoint.y), 2.0));

    }
    else if ([recognizer state] == UIGestureRecognizerStateChanged)
    {

        CGPoint point = [recognizer locationInView:vw_txtfield.superview];
        newdistance = sqrt(pow((vw_txtfield.frame.origin.x - point.x), 2.0) + pow((vw_txtfield.frame.origin.y - point.y), 2.0));


        float wChange = 0.0, hChange = 0.0;


        wChange = newdistance / olddistance ;//Slow down increment

        NSLog(@"wchange %f",wChange);


        hChange= newdistance / olddistance;

        NSLog(@"hchange %f",hChange);

        if (txt_LableText.font.pointSize<=6 && wChange<1) {
            return;
        }
        else
        {

            vw_txtfield.bounds = CGRectMake(vw_txtfield.bounds.origin.x, vw_txtfield.bounds.origin.y, vw_txtfield.bounds.size.width * (wChange), vw_txtfield.bounds.size.height * (hChange));
            [txt_LableText setContentScaleFactor:   newdistance / olddistance ];
            float int_NewFontsize= ( newdistance / olddistance)  * txt_LableText.font.pointSize;
            NSLog(@"font size %f",int_NewFontsize);
            [txt_LableText setFont:[UIFont fontWithName:txt_LableText.font.fontName size:int_NewFontsize]];

            prevPoint = [recognizer locationInView:vw_txtfield.superview];
            [vw_txtfield setNeedsDisplay];
            olddistance = sqrt(pow((vw_txtfield.frame.origin.x - point.x), 2.0) + pow((vw_txtfield.frame.origin.y - point.y), 2.0));
        }
    }
    else if ([recognizer state] == UIGestureRecognizerStateEnded)
    {

        prevPoint = [recognizer locationInView:vw_txtfield.superview];
        [vw_txtfield setNeedsDisplay];
     }
}

0 个答案:

没有答案