0

我有一个带有图像视图和标签的视图。图像视图显示不同大小的图像,它固定在顶部(见图) 我想动态地将文本标签粘贴到每个图像的底部,图像缩略图和 UILabel 之间没有任何空格。

4

2 回答 2

1

你可以通过编程来做到这一点。我假设您已将 UILabel 附加到“label”并将图像附加到“imageView”变量。

CGRect labelFrame = label.frame;

labelFrame.origin.y = imageView.frame.origin.y + imageView.frame.size.height + any_space_you_want_between_image_and_label;

label.frame = labelFrame;

这将改变标签在 imageView 下方的位置。我希望这有帮助。

于 2013-01-25T21:30:08.433 回答
1

因为正如您提到UIViewContentModeScaleAspectFit的,您使用内容模式解决方案有点困难。您必须实际计算图像的最终高度(UIImageView 内图像的实际大小):

    //UIImage *img = ...; UIImageView *imgView = ....
    CGFloat imageWidth = img.size.width;
    CGFloat imageHeight = img.size.height;
    CGFloat viewWidth = imgView.frame.size.width;
    CGFloat viewHeight = imgView.frame.size.height;
    float actualHeight = imageHeight * viewWidth / imageWidth;
// this is the actual height of the UIImage inside the UIImageView
    CGRect labelFrame = label.frame;
    labelFrame.origin.y = imageView.frame.origin.y + actualHeight + any_space_you_want_between_image_and_label;
    label.frame = labelFrame;
于 2013-01-25T22:00:09.173 回答