SwiftyAvatar.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // SwiftyAvatar.swift
  3. // SwiftyAvatar
  4. //
  5. // Created by Dimitrios Kalaitzidis on 04/08/16.
  6. // Copyright © 2016 Dimitrios Kalaitzidis. All rights reserved.
  7. //
  8. import UIKit
  9. @IBDesignable class SwiftyAvatar: UIImageView {
  10. @IBInspectable var roundness: CGFloat = 2 {
  11. didSet{
  12. layoutSubviews()
  13. }
  14. }
  15. @IBInspectable var borderWidth: CGFloat = 5 {
  16. didSet{
  17. layoutSubviews()
  18. }
  19. }
  20. @IBInspectable var borderColor: UIColor = UIColor.blue {
  21. didSet{
  22. layoutSubviews()
  23. }
  24. }
  25. @IBInspectable var background: UIColor = UIColor.clear {
  26. didSet{
  27. layoutSubviews()
  28. }
  29. }
  30. override func layoutSubviews() {
  31. super.layoutSubviews()
  32. layer.cornerRadius = bounds.width / roundness
  33. layer.borderWidth = borderWidth
  34. layer.borderColor = borderColor.cgColor
  35. layer.backgroundColor = background.cgColor
  36. clipsToBounds = true
  37. let path = UIBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), cornerRadius: bounds.width / roundness)
  38. let mask = CAShapeLayer()
  39. mask.path = path.cgPath
  40. layer.mask = mask
  41. }
  42. }