简体   繁体   English

如何快速使图像视图和标签相互适应

[英]How to make an image view and a label adaptive to each other in swift

I have a UI View, which consists of image view and a label.我有一个 UI 视图,它由图像视图和标签组成。 UI view has constraints set up to the superview. UI 视图具有为超级视图设置的约束。 These labels are of different sizes,(data coming from backend) so I want to adjust the background image according to the length of the label.这些标签大小不同,(数据来自后端)所以我想根据标签的长度调整背景图像。

For example : Label : "Hi How are you" In this scenario - the image should fit the label and should look proportionate to the screen例如:标签:“嗨,你好吗”在这种情况下 - 图像应该适合标签并且看起来应该与屏幕成比例

Label : "Hi" In the scenario - the image should fit the label containing just "Hi", meaning it should shrink based on the label string length.标签:“Hi” 在场景中 - 图像应该适合只包含“Hi”的标签,这意味着它应该根据标签字符串长度缩小。

Is this possible ?这可能吗 ?

Follow these steps:按着这些次序:

  1. Take your UIView into a UIScrollView or tableviewcell or collectionviewcell.将您的 UIView 放入 UIScrollView 或 tableviewcell 或 collectionviewcell。
  2. Give UIView constraint without height.给出没有高度的 UIView 约束。
  3. Take your UIImageView and UILabel inside UIView(don't give UILabel height constraint).把你的 UIImageView 和 UILabel 放在 UIView 里面(不要给 UILabel 高度约束)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM