在UIImageView上调整UILabel的大小

我的问题如下:我在UIImageView上面有一些标签。 图像是公式, UILabel表示条目。 如果我旋转设备, UIImageView的图像将通过选项“纵横比较”进行调整。 我怎样才能实现UILabel的大小调整和对齐正确? resize的常规选项无法正常工作。 UIImageView可以使用minimumZoomScale和maximumZoomScale设置进行缩放。

谢谢。

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

 label.transform = CGAffineTransformMakeScale(0.5, 0.5); 

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

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

其余的是数学=)


所以我设法解决了你的问题。 这里有完整的源代码。

基本上我删除了你的UIImageView并用UIView替换它。 然后我在UIView添加了你的UILabel ,并从界面构建器中的UIViewUILabel删除了任何自动调整。 这样当UIViewresize时,其中的任何内容也将被resize/重新定位。

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