[英]UILabel for UIImageView that has contentMode UIViewContentModeScaleAspectFit
I need to set a label that has a custom background color on top of a UIImageView. 我需要在UIImageView上设置一个具有自定义背景色的标签。 The problem is that I want it's width to be the exact size as the image from the image view is. 问题是我希望它的宽度与图像视图中的图像一样准确。 This is the code so far: 到目前为止,这是代码:
UIImageView *pictureV = [[UIImageView alloc] initWithFrame:CGRectMake(pictureX, 0, pictureSize.width, pictureSize.height)];
pictureV.image = self.picture.image;
pictureV.contentMode = UIViewContentModeScaleAspectFit;
And the label: 和标签:
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]
When setting the contentMode as UIViewContentModeScaleAspectFit, some images are smaller than my UIImageView and I can't seem to make the label's width the same size. 将contentMode设置为UIViewContentModeScaleAspectFit时,某些图像比我的UIImageView小,并且我似乎无法使标签的宽度相同。
If you use UIViewContentModeScaleAspectFit, the image will not take the full size of UIImage View. 如果您使用UIViewContentModeScaleAspectFit,则图像将不会采用UIImage View的完整大小。 Instead of this set your pictureV's contentMode property to UIViewContentModeScaleToFill , then the image will take the whole size of your imageView 而不是将pictureV的contentMode属性设置为UIViewContentModeScaleToFill,然后图像将采用imageView的整个大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.