NCAvatar.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // NCAvatar.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/11/2018.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. import Foundation
  9. @IBDesignable class NCAvatar: 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. required init?(coder aDecoder: NSCoder) {
  31. super.init(coder: aDecoder)
  32. }
  33. override init(frame: CGRect) {
  34. super.init(frame: frame)
  35. }
  36. override func layoutSubviews() {
  37. super.layoutSubviews()
  38. layer.cornerRadius = bounds.width / roundness
  39. layer.borderWidth = borderWidth
  40. layer.borderColor = borderColor.cgColor
  41. layer.backgroundColor = background.cgColor
  42. clipsToBounds = true
  43. let path = UIBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), cornerRadius: bounds.width / roundness)
  44. let mask = CAShapeLayer()
  45. mask.path = path.cgPath
  46. layer.mask = mask
  47. }
  48. }