My image is not coming out as a circle for some reason. What I did is defining the corner radius as half of the image width. Does this have to do something with the constraints I set for the image?
Image circle code:
profileImage.layer.cornerRadius = profileImage.layer.frame.width / 2
profileImage.clipsToBounds = true
profileImage.layer.borderWidth = 3.0
profileImage.layer.borderColor = UIColor.white.cgColor
What it looks like on storyboard:
What it looks like on iPhone:
swift 4:
#IBOutlet weak var photoImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
photoImage.layer.borderWidth = 1
photoImage.layer.masksToBounds = false
photoImage.layer.borderColor = UIColor.black.cgColor
photoImage.layer.cornerRadius = photoImage.frame.height/2
photoImage.clipsToBounds = true
}
Related
I am not able to get accurate shadow for collectionview cell which has a UIImage and a label.
I have collection view for which shadow appears properly. Below are code snippets for both and screenshot showing cell with problem and yellow collection view with proper shadow. I went through many posts but unable to get solution that can get shadow similar to collection view.
For Collection view that is embedded in UIView and proper shadow
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
enclosureView.layer.shadowPath = UIBezierPath(rect: enclosureView.bounds).cgPath
}
override func viewDidLoad() {
super.viewDidLoad()
enclosureView.layer.shadowColor = UIColor.systemGray2.cgColor
enclosureView.layer.shadowOpacity = 1
enclosureView.layer.shadowOffset = .zero
enclosureView.layer.shadowRadius = 5
enclosureView.layer.masksToBounds = false
enclosureView.cornerRadius = 10
}
For Collection view cell having UIImage and UILabel.
UIImage is embedded in UIView. I want shadow around image and not around cell.
class cell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.layer.masksToBounds = false
self.clipsToBounds = false
image.layer.cornerRadius = 10
image.clipsToBounds = true
image.backgroundColor = .clear
imageEnclosureView.layer.masksToBounds = false
imageEnclosureView.clipsToBounds = false
imageEnclosureView.layer.shadowRadius = 5
imageEnclosureView.layer.shadowOpacity = 1
imageEnclosureView.layer.shadowColor = UIColor.systemGray2.cgColor
imageEnclosureView.layer.shadowOffset = .zero
imageEnclosureView.backgroundColor = .clear
}
override func layoutSubviews() {
super.layoutSubviews()
self.imageEnclosureView.layer.shadowPath = UIBezierPath(
roundedRect: imageEnclosureView.bounds,
cornerRadius: 10
).cgPath
}
}
I'm calling a function that sets up a UIImageView:
func setupImageView(_ imageView: UIImageView) {}
I want to give that UIImageView an image, round its corners, and give it two different borders.
Here is what I am currently doing:
imageView.image = imageConstants.imageThatIsWanted
imageView.clipsToBounds = true
imageView.layer.cornerRadius = imageView.frame.height / 2
imageView.layer.borderWidth = 3.0
imageView.layer.borderColor = UIColor.white.cgColor
What is the best way to apply a second borderColor of color blue around the white border?
I tried creating a sublayer as a CALayer and giving it a blue border, but this goes behind the image, and also inside of the white border. I also tried drawing a UIBezierPath, but that stays inside of the white border as well.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var secondView: UIView!
#IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imgView.layer.cornerRadius = imgView.frame.size.height/2
secondView.layer.cornerRadius = secondView.frame.size.height/2
imgView.layer.borderWidth = 5.0
imgView.layer.borderColor = UIColor.red.cgColor
imgView.clipsToBounds = true
secondView.clipsToBounds = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You can add UIlabel or UIImageview at back of your image view having size little bit larger than your image view and applying corner radius, if you want to reduce line of your code (Please check below code)
imgview.layer.masksToBounds = true
imgview.layer.cornerRadius = imgview.frame.size.width/2
imgview.layer.borderWidth = 5
imgview.layer.borderColor = UIColor.white.cgColor
Add new image view programatically at back side of image view already taken
let img = UIImageView(frame: CGRect(x: imgview.frame.origin.x - 2, y: imgview.frame.origin.y - 2, width: imgview.frame.size.width + 4, height: imgview.frame.size.height + 4))
img.layer.masksToBounds = true
img.layer.cornerRadius = img.frame.size.width/2
//img.backgroundColor = UIColor.blue // You can also use background color instead of border
img.layer.borderWidth = 5
img.layer.borderColor = UIColor.blue.cgColor
self.view.addSubview(img)
self.view.sendSubview(toBack: img)
I know its not proper solution but we can use this to reduce lines of code
Hope it will helps you
I'm trying to create a border around a mask using a UIimage. When I create the mask in code it looks fine:
#IBOutlet weak var imageToBeMasked: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let mask = UIImageView(image: Image)
imageToBeMasked.mask = mask
}
But when I try and create a stroke around it appears to not use the mask at all:
#IBOutlet weak var imageToBeMasked: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let mask = UIImageView(image: Image)
// the layer used to mask the image view
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(rect: mask.bounds).cgPath
imageToBeMasked.layer.mask = maskLayer
// the layer used to draw the border
let strokeLayer = CAShapeLayer()
strokeLayer.frame = imageToBeMasked.bounds
strokeLayer.fillColor = UIColor.clear.cgColor
strokeLayer.path = UIBezierPath(rect: mask.bounds).cgPath
strokeLayer.strokeColor = UIColor.black.cgColor
strokeLayer.lineWidth = 4
imageToBeMasked.layer.addSublayer(strokeLayer)
}
Any clue on how to solve this issue?
The CALayer is not affected by the mask itself. The quickest way would be creating a new image which is slightly bigger than the original, place it behind the original image and tint it with a color.
let templateImage = originalImage.withRenderingMode(.alwaysTemplate)
strokeImageView.image = templateImage
strokeImageView.tintColor = .black
Hello i have working no error codes for UITextfield border color change but when using it in Swift 3 dont change textfield border color and dont gives error. I need your help my codes under below.
#IBOutlet weak var email: UITextField!
#IBOutlet weak var pass: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let myColor : UIColor = UIColor.white()
email.layer.borderColor = myColor.cgColor
pass.layer.borderColor = myColor.cgColor
}
Thank you !
You also need to set border width, because your border color is set already but your default border width is 0.0 so you can't see it.
So, set border width something like,
email.layer.borderWidth = 1.0
Update :
Your code should be like,
#IBOutlet weak var email: UITextField!
#IBOutlet weak var pass: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let myColor = UIColor.white
email.layer.borderColor = myColor.cgColor
pass.layer.borderColor = myColor.cgColor
email.layer.borderWidth = 1.0
pass.layer.borderWidth = 1.0
}
Use the below code in swift 3:
in view did load
outer_line.layer.borderWidth = 1
outer_line.layer.borderColor = UIColor.lightGray.cgColor
Try to use this,
It might be helpful to you
let myColor : UIColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
myTextField.layer.masksToBounds = true
myTextField.layer.borderColor = myColor.CGColor
myTextField.layer.borderWidth = 2.0
I think you should first provide a borderWidth
#IBOutlet weak var email: UITextField!
#IBOutlet weak var pass: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let myColor : UIColor = UIColor.white()
email.layer.borderWidth = 1
email.layer.borderColor = myColor.cgColor
pass.layer.borderColor = myColor.cgColor
}
and then set a color :)
Updated Swift 3 :
if you want to set the bottom border to UITextField, used below lines of code :
// function defination :
func setBottomBorderToTextFields() {
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0, y: yourTextFieldName.frame.height - 1, width: yourTextFieldName.frame.width, height: 1)
bottomLine.backgroundColor = UIColor.gray.cgColor // background color
yourTextFieldName.borderStyle = UITextBorderStyle.none // border style
yourTextFieldName.layer.addSublayer(bottomLine)
}
// In ViewDidLoad() :
self.setBottomBorderToTextFields()
You need to set the borderWidth from the UITextField's layer property.
Like: email.layer.borderWidth = 1.
Also, if you frequently need to set borders to your views, you can make an extension like this:
extension UIView {
func addBorderAndColor(color: UIColor, width: CGFloat, corner_radius: CGFloat = 0, clipsToBounds: Bool = false) {
self.layer.borderWidth = width
self.layer.borderColor = color.cgColor
self.layer.cornerRadius = corner_radius
self.clipsToBounds = clipsToBounds
}
}
Call this like:
email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true)
Since this method sets the borderWidth, it will also solve your problem.
It is working.
give border width
and after give bgcolor.
txtemail.layer.borderWidth = 1.0
txtemail.layer.borderColor = UIColor.blue.cgColor
It definitely works
Is there a way for UILabel to draw a border around itself? This is useful for me to debug the text placement and to see the placement and how big the label actually is.
You can set label's border via its underlying CALayer property:
#import <QuartzCore/QuartzCore.h>
myLabel.layer.borderColor = [UIColor greenColor].CGColor
myLabel.layer.borderWidth = 3.0
Swift 5:
myLabel.layer.borderColor = UIColor.darkGray.cgColor
myLabel.layer.borderWidth = 3.0
Here are some things you can do with UILabel and its borders.
Here is the code for those labels:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBOutlet weak var label5: UILabel!
#IBOutlet weak var label6: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// label 1
label1.layer.borderWidth = 1.0
// label 2
label2.layer.borderWidth = 5.0
label2.layer.borderColor = UIColor.blue.cgColor
// label 3
label3.layer.borderWidth = 2.0
label3.layer.cornerRadius = 8
// label 4
label4.backgroundColor = UIColor.cyan
// label 5
label5.backgroundColor = UIColor.red
label5.layer.cornerRadius = 8
label5.layer.masksToBounds = true
// label 6
label6.layer.borderWidth = 2.0
label6.layer.cornerRadius = 8
label6.backgroundColor = UIColor.yellow
label6.layer.masksToBounds = true
}
}
Note that in Swift there is no need to import QuartzCore.
See also
Border, rounded corners, and shadow on a CALayer
Explanation of masksToBounds
Using a border with a Bezier path for a layer
How to do transforms on a CALayer
Swift version:
myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.greenColor().CGColor
For Swift 3:
myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.green.cgColor
Swift 3/4 with #IBDesignable
While almost all the above solutions work fine but I would suggest an #IBDesignable custom class for this.
#IBDesignable
class CustomLabel: UILabel {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
#IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
#IBInspectable var borderWidth: CGFloat = 2.0 {
didSet {
layer.borderWidth = borderWidth
}
}
#IBInspectable var cornerRadius: CGFloat = 0.0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
}
UILabel properties borderColor,borderWidth,cornerRadius in Swift 4
#IBOutlet weak var anyLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
anyLabel.layer.borderColor = UIColor.black.cgColor
anyLabel.layer.borderWidth = 2
anyLabel.layer.cornerRadius = 5
anyLabel.layer.masksToBounds = true
}
You can use this repo: GSBorderLabel
It's quite simple:
GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithTextColor:aColor
andBorderColor:anotherColor
andBorderWidth:2];
Solution for Swift 4:
yourLabel.layer.borderColor = UIColor.green.cgColor
it really depends on how many boarder use in your view , sometimes , just add a UIVIEW which the size is a bit bigger to create the border . the method is faster than produce a view
Using an NSAttributedString string for your labels attributedText is probably your best bet. Check out this example.