在swift中更改背景图像代码的大小

[英]Change the size of the background image codewise from a label in swift


In the app that I am making I have some background images for some labels. The image is 500x550 pixels but my label is 250x275 pixels. How can I make the code change the width and height of the background so it fits the size of the label?

在我制作的应用中,我有一些标签的背景图片。图像是500x550像素,但我的标签是250x275像素。我如何使代码改变背景的宽度和高度,使它适合标签的大小?

Code I have now:

代码我已经:

lblMainNumber.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)

1 个解决方案

#1


1  

It is easier to have a container UIView with size of 250 x 275 that will contain 2 subviews with same size:

更容易拥有一个大小为250 x 275的容器UIView,它将包含两个大小相同的子视图:

1) a UIImageView with your image (set its contentMode to UIViewContentModeScaleAspectFit)

1)带有图像的UIImageView(将其内容模式设置为UIViewContentModeScaleAspectFit)

2) UILabel with your text. It will be on top of the UIImageView to stay visible.

2) UILabel和你的文本。它将在UIImageView的顶部以保持可见。

You would possibly want to set both subviews (via autolayout) to match the size of container view. Pattern image approach is pain in the ass and not wort the effort.

您可能希望设置两个子视图(通过autolayout)以匹配容器视图的大小。模式图像方法是痛苦的屁股,而不是努力。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/02/19/606282c7f9485c189d3c0567d9d3d2f1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com