How to stretch Image to fill the Label Width set in Background in UILabel?

前端 未结 2 688
暖寄归人
暖寄归人 2020-12-25 09:24

I have simple View based application. I had taken only UILabel on it.

Following is my code in viewDidLoad:

lblBack.textColor = [UIColo         


        
2条回答
  •  醉梦人生
    2020-12-25 09:50

    Porting over Maulik's answer to Swift 2.2, you get the following code:

    var img: UIImage = UIImage(named: "cabImage")!
    var imgSize: CGSize = testLabel.frame.size
    
    UIGraphicsBeginImageContext(imgSize)
    
    img.drawInRect(CGRectMake(0, 0, imgSize.width, imgSize.height))
    var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    
    UIGraphicsEndImageContext();
    
    testLabel.backgroundColor = UIColor(patternImage: newImage)
    

    Personally tested to be working!

提交回复
热议问题