It is Possible to set background image for a label - swift

I want to set background image for a label in my project but i dont know for how to set, please say it is possible or not?

This should do the trick:
yourLabel.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)

The UILabel control does not have background image or image property.
You can set image to UILabel using backgroundColor property as shown below :
cell.lblNumber!.text = ""
cell.lblNumber!.backgroundColor = UIColor(patternImage: UIImage(named: "OK")!)
Set the text to empty since it will display image and text on top of it.

Related

Using image for UITableView background; background still shows black

I've got a grey custom image that I'm using as my background for a UITableView. Image is PNG with the appropriate alpha settings, has rounded corners, and loads fine. Despite using the following, however, I still see a black "background" where the corners become transparent.
tableView.backgroundView = UIImageView(image: UIImage(named: "someImage"))
tableView.backgroundColor = UIColor.clear
I also set my TableView background to bright colors to see if that's where the issue was, however still black on the background.
How do I get this area to be completely transparent so it looks as if my round corners are the end of the TableView?
Thanks!
Try the snippet. Hope this works
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill

Swift shrink UIImageView according to what image it contains

I have an UIImageView which is as big as the whole view. When I insert an image, I would like for the image view to shrink itself in order for it to be as big as the image I insert itself. I cannot find a way to do it.
There are a number of ways you can approach this assuming you have a UIImageView declared like so:
#IBOutlet var dynamicImageView: UIImageView!
When changing your image, set the imageView size to that of the UIImage e.g:
func changeImage(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.frame.size = image.size
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Just use the .sizeToFit method e.g:
func changeImageAgain(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.sizeToFit()
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Running on an iPhone 7 Plus the UIImageView size is initially (375,647), and when applying either method the size changes to (800,665) which is the same size as the GitHub OctoCat:
I have created one demo app which will contains XIB with image view. Here I have written code to resize imageView as per image size. This code is working in my project, I shared link for demo app, you can download and test it.
In this project, I have implemented code to create imageView based on image size and vertically and horizontally centred aligned.
Also implemented code to zoom image.
https://app.box.com/s/sro2g7gdv5c67f9oj97gfhicjnpo2vqc
I hope this will work for you and as per your requirement.

UITextView background image is scrolling for multiple lines

I have created extension for UITextView, where I set image to the textview using below code.
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
self.addSubview(backgroundImage)
self.sendSubview(toBack: backgroundImage)
At start it looks fine as showing below image (left side), however as I add multi-line text, image also starts scrolling (right side), which is incorrect.
Any idea how to make image fixed as it is?
Below is what I did
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
mainView.insertSubview(backgroundImage, belowSubview: self)
mainView is nothing but the main view (self.view) of view controller.
This is like we are not going to add backgroundImage in the subview of textview, instead we will be adding in the mainview (self.view).
A simple solution:
A container UIView contains background UIImageView and TextView(with clear background color).

ContentView background colour Swift

When I select a cell on a table view it by default changes it's ContentView background colour to a light grey and I am trying to override this.
And I am using the following code to remove this grey colour from the selection. But it is removing the background colour of everything (UIViews, UIImageViews) inside that cell.
Is there a way to eliminate the background colour of the ContentView only?
let changeselectionColor = UIView()
changeselectionColor.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = changeselectionColor
I have the following table view cell.
This is how it should look like when selected
This is how it end up looking with all BGs transparent
You don't need to override anything to achieve the effect, actually UITableViewCell provides the API to remove the highlight gray:
cell.selectionStyle = UITableViewCellSelectionStyle.None

Title Image UINavigationBar

I'm trying to add a title image to my nav bar in the entirety of my app. The below line of code in my App Delegate works, but the image is tiled to fit... Is there a way I can set the dimensions and position?
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "tmp logo.png"), forBarMetrics: .Default)
You can set the size of image, by setting the size of UIImageView.
var imageNew = UIImageView(frame: CGRectMake(10, 10, 100, 100));// define
the size of the view here.
var image = UIImage(named: "myImage.png");
imageNew.image = image;
self.view.addSubview(imageNew);
Pl. refer to the below stackoverflow response, which provides the solution
How do set a width and height of an Image in Swift