简体   繁体   English

具有contentMode UIViewContentModeScaleAspectFit的UIImageView的UILabel

[英]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.

相关问题 当contentMode UIViewContentModeScaleAspectFit时,禁用UIImageView调整大小的行为 - Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit 在将contentMode设置为UIViewContentModeScaleAspectFit时,如何设置UIImageView左对齐或右对齐? - how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit? 如何避免使用UIImageView的contentmode UIViewContentModeScaleAspectFit保持高度恒定而损失图像质量? - How to avoid losing image quality using UIImageView's contentmode UIViewContentModeScaleAspectFit keeping height constant? UIImageView contentMode页首 - UIImageView contentMode Top 设置UIImageView的contentMode - Set contentMode of UIImageView UIImageView上的contentMode(scaleAspectFill)-Swift - contentMode (scaleAspectFill) on UIImageView - Swift UIImageView 忽略 contentMode 的变化 - UIImageView ignores changes in contentMode UIImageView 中的 UIViewContentModeScaleAspectFit 无法正常工作 - UIViewContentModeScaleAspectFit in UIImageView does not work correctly UIViewContentModeScaleAspectFit不适用于UIScrollView子视图UIImageView - UIViewContentModeScaleAspectFit not working for UIScrollView subview UIImageView 使用UIViewContentModeScaleAspectFit在UIImageView中调整图像 - Fit image in UIImageView using UIViewContentModeScaleAspectFit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM