Question

My problem is as follows: I have on top of a UIImageView some labels. The image is a formular and the UILabels represents the entries. If I rotate the device the image inside UIImageView gets resized with option "aspect fit". How can I achieve that the UILabel is also resized and aligned correct? The normal options for resizing aren't working as needed. The UIImageView is zoomable with minimumZoomScale and maximumZoomScale set.

Thanks.

Was it helpful?

Solution

You can resize a UILabel proportionally using:

label.transform = CGAffineTransformMakeScale(0.5, 0.5);

You can reposition a UILabel relatively using:

label.frame = CGRectOffset(label.frame, deltaX, deltaY);

The rest is maths =)


So I managed to solve your problem. Full source code here.

Basically I removed your UIImageView and replaced it with a UIView. Then I added your UILabels within the UIView and removed any autosizing from the UIView and UILabels in interface builder. That way when the UIView gets resized any content in it will get resized/repositioned as well.

The next step was to change the class of the UIView to UIImageView in interface builder. Now from the source code I could set an image to it using imgView.image = [UIImage imageNamed:@"imagename.png"]; as well as I could set the mode using imgView.contentMode = UIViewContentModeScaleAspectFit;.

The transformation at rotation now happens with:

if (UIDeviceOrientationIsLandscape(deviceOrientation)){

    // Play with the scale if you have any other images with different ratio.
    float scale = 2.3f/3.0f;

    // Transforming the UIImageView containing the UILabels which actually is a simple UIView
    imgView.transform = CGAffineTransformMakeScale(scale, scale);

} else {

    // Original size again
    imgView.transform = CGAffineTransformMakeScale(1, 1);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top