user2948459
user2948459

Reputation: 37

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?

Code I have now:

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

Upvotes: 2

Views: 969

Answers (1)

Earl Grey
Earl Grey

Reputation: 7466

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

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

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

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.

Upvotes: 1

Related Questions