我需要在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小,我似乎无法使标签的宽度相同。
答案 0 :(得分:1)
如果使用UIViewContentModeScaleAspectFit,图像将不会占用UIImage View的完整大小。 而不是将你的pictureV的contentMode属性设置为UIViewContentModeScaleToFill, 然后图像将采用imageView的整个大小