【问题标题】:Resize UILabel correct on top of UIImageView在 UIImageView 上正确调整 UILabel 的大小
【发布时间】:2012-02-11 00:46:15
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: ios xcode uiimageview uilabel aspect-ratio


    【解决方案1】:

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

    label.transform = CGAffineTransformMakeScale(0.5, 0.5);
    

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

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

    剩下的就是数学 =)


    所以我设法解决了你的问题。完整源码here.

    基本上我删除了你的UIImageView 并用UIView 替换它。然后我在UIView 中添加了您的UILabels,并从界面构建器中的UIViewUILabels 中删除了任何自动调整大小。这样,当 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);
    
    }
    

    【讨论】:

    • 所以解决方案是注册设备旋转并在该功能内使用您的两个命令?
    • 是的。但为了做到这一点,您需要计算新的位置和大小。是基本几何。笔和纸是你最好的朋友。
    • 我可以得到aspect fit动作的比例吗? UILabels 与 UIImageView 位于同一子视图中,因此它们在旋转后被调整大小。如果我停用自动调整大小,问题是两者的缩放都无法按预期工作。除了label.transformlabel.frame,就没有别的可能了吗?
    • 那么 iPhone 的屏幕分辨率是 960 像素 x 640 像素。这给了我们一个 3:2 的比例。假设您希望在从垂直视图旋转到水平视图时获得适合的方面。您的垂直尺寸为 960px,旋转后将转换为 640px。这意味着您的包含视图应垂直转换 2/3 和水平转换 3/2。您正在寻找的命令是:label.transform = CGAffineTransformMakeScale(3/2, 2/3);。你对旋转做同样的事情,但是切换比例:label.transform = CGAffineTransformMakeScale(2/3, 3/2);
    • 我认为我试图解释的问题并不完全清楚:) 例如你有以下形式:megasinnlos.de/images/vorschau/ausweis.jpg'Kuh'和'Muh'是我的两个UILabels。 iPhone是横向的,现在我以纵向模式旋转。现在UIImageView 是Aspect Fit,所以左侧和右侧会有空格(纵向模式)。图像的宽度现在比以前小,标签的位置和比例必须采用。如何做到这一点?谢谢。
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多