在 UIImageView 之上调整 UILabel 的大小

发布于 2024-12-26 20:37:53 字数 281 浏览 2 评论 0原文

我的问题如下: 我在 UIImageView 之上有一些标签。该图像是一个公式,UILabel 代表条目。 如果我旋转设备,UIImageView 内的图像将使用“宽高比适合”选项调整大小。 如何才能使 UILabel 也调整大小并正确对齐? 调整大小的正常选项无法按需要工作。 UIImageView 可通过设置minimumZoomScale 和maximumZoomScale 进行缩放。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2025-01-02 20:37:53

您可以使用以下方法按比例调整 UILabel 的大小:

label.transform = CGAffineTransformMakeScale(0.5, 0.5);

您可以使用相对地重新定位 UILabel 的位置:

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

剩下的就是数学 =)


所以我设法解决了您的问题。完整源代码此处

基本上,我删除了您的 UIImageView 并将其替换为 UIView。然后,我在 UIView 中添加了您的 UILabel,并从界面生成器中的 UIViewUILabel 中删除了任何自动调整大小。这样,当 UIView 调整大小时,其中的任何内容也会调整大小/重新定位。

下一步是在界面构建器中将 UIView 的类更改为 UIImageView。现在,从源代码中,我可以使用 imgView.image = [UIImage imageNamed:@"imagename.png"]; 为其设置图像,也可以使用 imgView 设置模式。 contentMode = UIViewContentModeScaleAspectFit;。

旋转变换现在发生在:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文