在UIImageView上调整UILabel的大小 [英] Resize UILabel correct on top of UIImageView

查看:128
本文介绍了在UIImageView上调整UILabel的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:
我有一个 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.

谢谢。

推荐答案

您可以按比例调整 UILabel 的大小:

You can resize a UILabel proportionally using:

label.transform = CGAffineTransformMakeScale(0.5, 0.5);

您可以使用以下方法重新定位 UILabel

You can reposition a UILabel relatively using:

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

其余的是数学=)

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

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

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

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.

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

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

}

这篇关于在UIImageView上调整UILabel的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆