2013-05-07 145 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); 

    } 
} 

我希望它工作正常,爲您的代碼。謝謝