如何使标签的背景图像适合标签的框架大小

How to fit background image of label to the frame size of a label

self.backgroundColor = UIColor(patternImage: UIImage(named: "na72.png")!)

(UILABEL)

如何使标签的背景图片适合标签的边框大小? 我应该使用 'self.backgroundColor = UIColor(~~)'

因为您正在使用 patternimage 创建一个 UIColor,它被设计为创建一个瓷砖图案。

要解决此问题,您可以手动调整图像大小以匹配标签框架的大小,或使用函数(如 this one),然后将调整后的图像用作 patternimage.

使用上述函数的示例:

let image = imageWithImage(image: UIImage(named: "na72.png")!, scaledToSize: label.frame.size)
label.backgroundColor = UIColor(patternImage: image)

这个问题也被讨论过