简体   繁体   English

调整UILabel大小正确在UIImageView顶部

[英]Resize UILabel correct on top of UIImageView

My problem is as follows: I have on top of a UIImageView some labels. 我的问题如下:我在UIImageView有一些标签。 The image is a formular and the UILabel s represents the entries. 该图像是一个公式编制器, UILabel表示条目。 If I rotate the device the image inside UIImageView gets resized with option "aspect fit". 如果我旋转设备,则UIImageView的图像将使用选项“宽高比”调整大小。 How can I achieve that the UILabel is also resized and aligned correct? 如何实现UILabel也已调整大小和正确对齐? The normal options for resizing aren't working as needed. 调整大小的常规选项无法按需工作。 The UIImageView is zoomable with minimumZoomScale and maximumZoomScale set. UIImageView是可缩放的,其中设置了minimumZoomScale和maximumZoomScale。

Thanks. 谢谢。

You can resize a UILabel proportionally using: 您可以使用以下方法按比例调整UILabel大小:

label.transform = CGAffineTransformMakeScale(0.5, 0.5);

You can reposition a UILabel relatively using: 您可以使用以下方法相对地重新定位UILabel

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 . 基本上,我删除了您的UIImageView并将其替换为UIView Then I added your UILabel s within the UIView and removed any autosizing from the UIView and UILabel s in interface builder. 然后,我在UIView添加了UILabel ,并从界面构建器的UIViewUILabel删除了任何自动调整大小的功能。 That way when the UIView gets resized any content in it will get resized/repositioned as well. 这样,当UIView调整大小时,其中的任何内容也将被调整大小/重新定位。

The next step was to change the class of the UIView to UIImageView in interface builder. 下一步是在界面构建器中将UIView的类更改为UIImageView Now from the source code I could set an image to it using imgView.image = [UIImage imageNamed:@"imagename.png"]; 现在,从源代码中,我可以使用imgView.image = [UIImage imageNamed:@"imagename.png"];设置图像imgView.image = [UIImage imageNamed:@"imagename.png"]; as well as I could set the mode using imgView.contentMode = UIViewContentModeScaleAspectFit; 以及我可以使用imgView.contentMode = UIViewContentModeScaleAspectFit;设置模式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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM