【问题标题】:UILabel for UIImageView that has contentMode UIViewContentModeScaleAspectFit具有 contentMode UIViewContentModeScaleAspectFit 的 UIImageView 的 UILabel
【发布时间】:2012-09-14 12:48:08
【问题描述】:

我需要在 UIImageView 上设置一个具有自定义背景颜色的标签。问题是我希望它的宽度与图像视图中的图像完全相同。这是到目前为止的代码:

UIImageView *pictureV = [[UIImageView alloc] initWithFrame:CGRectMake(pictureX, 0, pictureSize.width, pictureSize.height)];
pictureV.image = self.picture.image;
pictureV.contentMode = UIViewContentModeScaleAspectFit;

还有标签:

UILabel *lblName = [[UILabel alloc]init];
lblName.backgroundColor=[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.6];
lblName.frame = CGRectMake(pictureV.bounds.origin.x, pictureV.bounds.origin.y + pictureV.bounds.size.height - 30, self.picture.image.size.width, 30);
[lblName setText:self.pictureNote.text];
[lblName setTextAlignment:UITextAlignmentCenter];
[lblName setTextColor:[UIColor whiteColor]];
[pictureV addSubview:lblName]

将 contentMode 设置为 UIViewContentModeScaleAspectFit 时,一些图像比我的 UIImageView 小,我似乎无法使标签的宽度相同。

【问题讨论】:

    标签: ios contentmode


    【解决方案1】:

    如果你使用 UIViewContentModeScaleAspectFit,图片将不会占据 UIImage View 的全尺寸。 而不是将您的pictureV 的 contentMode 属性设置为 UIViewContentModeScaleToFill , 然后图像将占用您的 imageView 的整个大小

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多