2013-05-07 190 views
2

我写这篇文章的代码在我的应用程序功能:放大和缩小在IOS

- (IBAction)ZoomInFunction{ 
@try{ 
    UITextField *test = (UITextField *)[self.view viewWithTag:indexNews]; 
    NSLog(@"INDEX NEWS : %d", indexNews); 

    UIFont *font = test.font; 
    if(test.font == [font fontWithSize:22]) 
     test.font = [font fontWithSize:22]; 
    else 
     test.font = [font fontWithSize:font.pointSize+2]; 
}@catch (NSException *err) { 
    NSLog(@"Error handler : %@", err); 
} 

}

- (IBAction)ZoomOutFunction{ 
@try { 
    UITextField *test = (UITextField *)[self.view viewWithTag:indexNews]; 

    UIFont *font = test.font; 
    if(test.font == [font fontWithSize:14]) 
     test.font = [font fontWithSize:14]; 
    else 
     test.font = [font fontWithSize:font.pointSize-2]; 
}@catch (NSException *err) { 
    NSLog(@"Error handler : %@", err); 

}

有时代码运行良好,但有时它的显示一个错误就是这样说的。

错误处理: - [UIView的字体]:无法识别的选择发送到实例0xac70780

回答

1

阅读这篇文章的苹果仔细,你会了解变焦和IOS缩小功能。

这里是行动代码:

 UITextView *textView = [UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)]; 
    UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeFontSize:)]; 
    [textView addGestureRecognizer:gestureRecognizer]; 

- (void)changeFontSize:(UIPinchGestureRecognizer *)gestureRecognizer 
{ 
    UITextView *textView = (UITextView *)gestureRecognizer.view; 
    float yourFontSize = gestureRecognizer.scale * FONT_SIZE; 
    textView.font = [UIFont systemFontOfSize:yourFontSize]; 
} 

http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

+0

这不是他所做的。他修改了文本字段的字体大小。他没有放大一切。所以我不确定这是否适用:) – 2013-05-07 09:31:58

+0

你想放大textview或什么告诉我? – Jitendra 2013-05-07 09:44:51

+0

我不,他是增加/减少字体大小。他没有使用滚动视图,所有 – 2013-05-07 09:45:35

1

缩放可以在UITextView的可能。在输入文本时动态扩展的UITextView,以及在用户捏住屏幕时缩放(类似的行为可以在TinyPost中找到)。

在viewDidLoad中

应用此
UITextView *test = (UITextView *)[self.view viewWithTag:indexNews]; 

    UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleTextView:)]; 

    [test addGestureRecognizer:gestureRecognizer]; 

和应用zoomIn和缩小的UITextView这样

- (void)scaleTextView:(UIPinchGestureRecognizer *)pinchGestRecognizer{ 

    CGFloat scale = pinchGestRecognizer.scale; 

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

    [self textViewDidChange:createTextView];  
} 

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

我希望它为你工作。

+0

感谢您的回复@chandan。 但我想从按钮点击,而不是从捏手势做到这一点。 – Template09 2013-05-08 03:58:21

+0

Hi @ Template09。看到我的另一个解决你的问题的答案,并将提供一个更好的和无错误的方法。 – chandan 2013-05-08 07:35:43

0

我已经应用它,我得到了适当的解决方案。这是由于这下面的代码行

UITextField *test = (UITextField *)[self.view viewWithTag:indexNews]; 

当您通过viewWithTag从self.view获取UITextView的一段时间:indexNews那么这个indexNews标签值已经分配给其他的UIView所以这行代码获取一个UIView,而不是的UITextField。所以它不能通过UIView调用Font方法,所以它返回错误。我有一个更好的解决方案,这

在您的.h文件中实现一个出路的UITextView

@property (weak, nonatomic) IBOutlet UITextView *fontTextView; 

在.m文件

@synthesize fontTextView; 

- (IBAction)ZoomInFunction{ 
    @try{ 

     UIFont *font = fontTextView.font; 
     if(fontTextView.font == [font fontWithSize:22]) 
      fontTextView.font = [font fontWithSize:22]; 
     else 
      fontTextView.font = [font fontWithSize:font.pointSize+2]; 
    }@catch (NSException *err) { 
     NSLog(@"Error handler : %@", err); 
    } 

} 

- (IBAction)ZoomOutFunction{ 
    @try { 

     UIFont *font = fontTextView.font; 
     if(fontTextView.font == [font fontWithSize:14]) 
      fontTextView.font = [font fontWithSize:14]; 
     else 
      fontTextView.font = [font fontWithSize:font.pointSize-2]; 
    }@catch (NSException *err) { 
     NSLog(@"Error handler : %@", err); 

    } 
} 

我希望它工作正常,为您的代码。谢谢