Adding Background image to UILabel
Try doing it with code:
Objective-C:
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blah"]];
Swift:
theLabel.backgroundColor = UIColor(patternImage: UIImage(named: "blah")!)
Or place an UIImageView
behind the label (not recommended).
Update: placing an UIImageView behind a label was not recommended because then you would have to manage two views. But if you must do something that only an UIImageView can do this is still a good approach. I suspect that your app will actually run better this way.
Set Background Image to UILabel Properly in iOS
You may use a custom button rather than label having no action against button.
For that you can use this piece of code to set the background image, and it will be stratched automatically.
[yourButton setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
Although There is no action against this button, you may also disable user intraction on this button as
yourButton.userInteractionEnabled = NO;
It is Possible to set background image for a label
This should do the trick:
yourLabel.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)
Adding a background image to a UILabel
Anyhow I solved this by inheriting UITextField and adding the subview to that. This works since there's actually a UILabel subview within this control and adding it behind it naturally solves the problem.
Also, I disabled user interaction so that it behaves like a label.
how to set a background image inside the UILabel inside a UITableViewCell
You could use autolayout instead of frames like this:
labe.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
labe.leftAnchor.constraint(equalTo: cell.leftAnchor),
labe.rightAnchor.constraint(equalTo: cell.rightAnchor),
labe.topAnchor.constraint(equalTo: cell.topAnchor),
labe.bottomAnchor.constraint(equalTo: cell.bottomAnchor),
])
You'll want to try to make sure the constraints are only added once.
Or use masks:
labe.autoresizingMask = [.flexibleWidth, .flexibleHeight]
How to set a background image to label
You can't add a background image to a UILabel, but you can add a UIImageView as a subview to a UILabel. Make sure the UILabel has backgroundColor = [UIColor clearColor];
for transparency.
E.g.
UIImageView *labelBackground = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"mybg.png"]];
[myLabel addSubview:labelBackground];
[labelBackground release];
myLabel.backgroundColor = [UIColor clearColor];
Should work. Untested.
How to set the label background image scale to fit in Swift?
You can't use a pattern color for this. Use UIImageView
to show the background image. Add a property to your custom cell class to hold a reference to the image view:
class ChallengeCell: UITableViewCell {
let messageBackgroundView = UIImageView()
override func layoutSubviews() {
if messageBackgroundView.superview != message.superview {
message.superview.insertSubview(messageBackgroundView, belowSubview: message)
messageBackgroundView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
messageBackgroundView.leadingAnchor.constraint(equalTo: message.leadingAnchor),
messageBackgroundView.trailingAnchor.constraint(equalTo: message.trailingAnchor),
messageBackgroundView.topAnchor.constraint(equalTo: message.topAnchor),
messageBackgroundView.bottomAnchor.constraint(equalTo: message.bottomAnchor),
])
messageBackgroundView.contentMode = .scaleToFill
}
super.layoutSubviews()
}
...
}
When you want to set the message background, set the image view's image:
challengeCell.messageBackgroundView.image = UIImage(named: "challengeMessage")
Swift: Make an image in background of label
Try this code: Tested in Swift 3.
Answer 1:
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
label.frame = CGRect(x:0, y:0, width:imageView.frame.width , height:imageView.frame.height)
label.text = "Some Title"
label.textAlignment = .center
label.textColor = .yellow
label.font = UIFont(name: "HelveticaNeue-medium", size: CGFloat(20))
imageView.addSubview(label)
}
Output:
Answer 2: Updated
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.layer.cornerRadius = 10
imageView.layer.masksToBounds = true
let label = UILabel()
label.frame = CGRect(x:0, y:0, width:imageView.frame.width , height:imageView.frame.height)
label.text = "Some Title"
label.textAlignment = .center
label.textColor = .yellow
label.layer.borderColor = UIColor.red.cgColor
label.layer.cornerRadius = 10
label.layer.masksToBounds = true
label.layer.borderWidth = 5
label.font = UIFont(name: "HelveticaNeue-medium", size: CGFloat(26))
imageView.addSubview(label)
}
Note: You need to workout the image size and label font size.
Output:
iOS How to set background image for text in UILabel/UITextView?
I've fixed the question as @Jecky Modi's suggestion, please refer to here for it.
Related Topics
How to Force My Keyboard to Be Up on My Program's Start in Swift
Opening a PDF Document When Clicking a Button
Shake Animation for Uitextfield/Uiview in Swift
iOS App, Programmatically Get Build Version
Bringing a Subview to Be in Front of All Other Views
What Happens with Constraints When a View Is Removed
How to Add Live Camera Preview to Uiview
Objective-C Delay Action with Blocks
Could Not Instantiate Class Named Ibnslayoutconstraint
How to Make Cmake Use Specific Compiler and Flags When Final Compilation Stage Instead of Detection
How to Return Value After the Execution of the Block? Swift
Change Button Image When Play Completes Swift
Cabasicanimation Does Not Animate Correctly When I Update Model Layer
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
How to Get the Size of a Scaled Uiimage in Uiimageview
Uinavigationcontroller Without Navigation Bar