netadictos
netadictos

Reputation: 7722

How to resize UILabel text after zooming UIScrollview zoomable

I have an UIScrollview that is zoomable, the subview is one UIView (viewTexto) that contains an UILabel inside (messageLabel).

This is the code

- (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)];

    }

With this code I can zoom, and the text is not blurry, there is no horizontal scroll but the size of the UILabel continues to be too large, so it is cut. I need that the width of the UILabel adopts to the scrollView width again as at the beginning.

I have read any question about UIScrollViews in SO and having found exactly what I need.

Upvotes: 2

Views: 3147

Answers (2)

netadictos
netadictos

Reputation: 7722

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.

Upvotes: 2

Ismael
Ismael

Reputation: 3937

After you zoom, the contentSize of a UIScrollView increases proportionally (originalContentSize * zoomScale), so when you adjust it again, it will become smaller

Example: Your scrollView.frame is (0, 0, 100, 100), and your contentSize is (100, 100). After you zoom to 2.0, your contentSize will be (200, 200). You are then putting it at (100, 100) manually, which equals to a contentSize of (50, 50) without zoom.

Since (contentSize.width <= scrollView.frame.size.width), there will be no horizontal scrolling, and the right-most part of your label is not visible.

Try not adjusting the contentSize. The scrolling will be possible then, and the user can reach the whole of the label

Edit: Re-reading your question, if you want the label to adjust and become visible entirely after the zoom, what you have to do in addition to adjusting the contentSize, is adjusting the messageLabel's frame. The label should wrap itself properly

Upvotes: 0

Related Questions