Mahesh Prasad Mahalik
Mahesh Prasad Mahalik

Reputation: 141

How to add a image of a Label's part of text background?

Below is the sample code to add a background colour of a attributed string but i want to set a lable attributed string colour to a image. Is there any way to do this?

let attributedString = NSMutableAttributedString(string:text!)
        attributedString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.red , range: range)

Upvotes: 2

Views: 741

Answers (3)

Andrew Bogaevskyi
Andrew Bogaevskyi

Reputation: 2615

You can simply use textColor property of UILabel

label.textColor = UIColor(patternImage: UIImage(named:"Pattern.jpg")!)

Upvotes: 0

Dibyajyoti Mondal
Dibyajyoti Mondal

Reputation: 86

UIColor has a method called "patternImage" where we can set image

 let attributedString = NSMutableAttributedString(string:text!)
           attributedString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor(patternImage: UIImage(named: "bg.png")!) , range: range)
           label.attributedText = attributedString

Upvotes: 3

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16416

You want to get label's attributed text and apply background color to UIImageView

You can fetch attributed text apply to label by label.attributedText and from that you can get NSBackgroundColorAttributeName and apply to you ImageView

Upvotes: 0

Related Questions