2

我已经在我的应用程序中编写了这段代码:

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

4

3 回答 3

1

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

在 viewDidLoad 中应用它

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

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

    [test addGestureRecognizer:gestureRecognizer];

并像这样在 UITextView 上应用 zoomIn 和 out

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

我希望这个对你有用。

于 2013-05-07T12:19:33.723 回答
1

仔细阅读这篇 Apple 文章,您将了解 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

于 2013-05-07T06:37:58.897 回答
0

我已经应用它并且我得到了正确的解决方案。这是由于以下代码行

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

有时,当您通过 viewWithTag:indexNews 从 self.view 获取 UITextView 时,此 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);

    }
}

我希望它适用于您的代码。谢谢

于 2013-05-08T05:52:05.567 回答