2

我有一个可缩放的 UIScrollview,子视图是一个 UIView (viewTexto),其中包含一个 UILabel (messageLabel)。

这是代码

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollViewtmp{

    return viewTexto;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollViewtmp withView:(UIView *)view atScale:(float)scale{

        messageLabel.contentScaleFactor=scale;

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, messageLabel.frame.origin.y + messageLabel.frame.size.heightt)];

    }

用这段代码我可以放大,而且文字不模糊,没有水平滚动但是UILabel的尺寸仍然太大,所以被剪切了。我需要 UILabel 的宽度与开始时一样再次采用 scrollView 宽度。

我已经阅读了关于 SO 中 UIScrollViews 的任何问题,并找到了我所需要的。

4

2 回答 2

2

OK, so finally I found the answer as Ismael suggested I had to adjust the width, the problem was to find the equation.

The way the scaling works and the width of the scrollview's subviews is not obvious at the beginning.

Once you scale a UIView in a scrollview, and you want to have the same width for it, you have to divide the width of the element by the scale.

That is to say that if your initial width was 600, once you scale you can think that the width has changed and you only have to resize it again to 600, but it is not true. 600 is going to be multiplied by the scale automatically. So the right answer would be to resize it to 600 / scale. Here we divide.

Now the code:

Everything happens in the method:

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollViewtmp withView:(UIView *)view atScale:(float)scale{}

The first thing was to get rid of the blurry fonts:

 messageLabel.contentScaleFactor=scale;

In another method I saved the initial width of the UILabel messageLabel (inside scrollview), I called the variable "initialWidth", that was 600. This is important, because I started using the current messageLabel width once in the scrollViewDidEndZooming method.

To readjust the width of the subviews of the scrollview to 600, we only have to divide the initial width by the zoomscale, and readjust the label:

[messageLabel setFrame:CGRectMake(0,0,(initialWidth/scale), messageLabel.frame.size.height)];
[messageLabel sizeToFit];

At this point, we have a scrollview that can be zoomed, with a Label that readjust the text to the initial width of the scrollview, we don't have horizontal scrollbars, but we have a problem, the vertical scrollbars have a wrong height: we can only scroll a part of the text.

This was a difficult second problem to solve. If you pass the messageLabel height to the contentsize, curiously it seems that it doesn'work, and even if I get a height multiplied by the scale in my NSLogs, it does not change the height of the scrolling, as if internally it was divided again. For example, if the initial height was 500, after scaling by 2, I get 1000 height, if I pass this value to the ContentSize it remains the same, as if it was divided again by 2.

So the solution was this time to multiply by the scale.

We only have to add this line:

   [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, (messageLabel.frame.origin.y+messageLabel.frame.size.height)*scale)];

As it is seen, the difficult part was to understand this mess with dividing or multiplying by the scale.

于 2012-11-23T16:56:16.937 回答
0

缩放后,contentSizea的aUIScrollView按比例增加(originalContentSize * zoomScale),所以当你再次调整它时,它会变小

示例:你scrollView.frame的是 (0, 0, 100, 100),你的contentSize是 (100, 100)。放大到 2.0 后,您contentSize将变为 (200, 200)。然后,您手动将其置于 (100, 100) 处,这等于 (50, 50) 的 a contentSizeof (50, 50) 没有缩放。

从 ( contentSize.width <= scrollView.frame.size.width) 开始,将不会有水平滚动,并且标签的最右侧部分不可见。

尽量不要调整contentSize。然后可以滚动,并且用户可以到达整个标签

编辑:重新阅读您的问题,如果您希望标签在缩放后调整并完全可见,除了调整 , 之外,您还需要contentSize调整messageLabel's框架。标签应正确包裹自己

于 2012-11-23T13:58:10.680 回答