// // SwiftyAvatar.swift // SwiftyAvatar // // Created by Dimitrios Kalaitzidis on 04/08/16. // Copyright © 2016 Dimitrios Kalaitzidis. All rights reserved. // import UIKit @IBDesignable class SwiftyAvatar: UIImageView { @IBInspectable var roundness: CGFloat = 2 { didSet{ layoutSubviews() } } @IBInspectable var borderWidth: CGFloat = 5 { didSet{ layoutSubviews() } } @IBInspectable var borderColor: UIColor = UIColor.blue { didSet{ layoutSubviews() } } @IBInspectable var background: UIColor = UIColor.clear { didSet{ layoutSubviews() } } override func layoutSubviews() { super.layoutSubviews() layer.cornerRadius = bounds.width / roundness layer.borderWidth = borderWidth layer.borderColor = borderColor.cgColor layer.backgroundColor = background.cgColor clipsToBounds = true let path = UIBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), cornerRadius: bounds.width / roundness) let mask = CAShapeLayer() mask.path = path.cgPath layer.mask = mask } }