|
@@ -2,14 +2,33 @@ import UIKit
|
|
|
|
|
|
class InitialsBadge: UIView {
|
|
|
|
|
|
+ private let verificationViewPadding = CGFloat(2)
|
|
|
+
|
|
|
private var label: UILabel = {
|
|
|
let label = UILabel()
|
|
|
label.adjustsFontSizeToFitWidth = true
|
|
|
label.textAlignment = NSTextAlignment.center
|
|
|
label.textColor = UIColor.white
|
|
|
+ label.translatesAutoresizingMaskIntoConstraints = false
|
|
|
return label
|
|
|
}()
|
|
|
|
|
|
+ private var verifiedView: UIImageView = {
|
|
|
+ let imgView = UIImageView()
|
|
|
+ let img = UIImage(named: "verified")
|
|
|
+ imgView.isHidden = false
|
|
|
+ imgView.image = img
|
|
|
+ imgView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
+ return imgView
|
|
|
+ }()
|
|
|
+
|
|
|
+ private var imageView: UIImageView = {
|
|
|
+ let imageViewContainer = UIImageView()
|
|
|
+ imageViewContainer.clipsToBounds = true
|
|
|
+ imageViewContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
+ return imageViewContainer
|
|
|
+ }()
|
|
|
+
|
|
|
convenience init(name: String, color: UIColor, size: CGFloat) {
|
|
|
self.init(size: size)
|
|
|
setName(name)
|
|
@@ -23,21 +42,32 @@ class InitialsBadge: UIView {
|
|
|
|
|
|
init(size: CGFloat) {
|
|
|
super.init(frame: CGRect(x: 0, y: 0, width: size, height: size))
|
|
|
- let initialsLabelCornerRadius = size / 2
|
|
|
- layer.cornerRadius = initialsLabelCornerRadius
|
|
|
+ let radius = size / 2
|
|
|
+ layer.cornerRadius = radius
|
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
|
heightAnchor.constraint(equalToConstant: size).isActive = true
|
|
|
widthAnchor.constraint(equalToConstant: size).isActive = true
|
|
|
- clipsToBounds = true
|
|
|
- setupSubviews()
|
|
|
+ setupSubviews(with: radius)
|
|
|
}
|
|
|
|
|
|
- private func setupSubviews() {
|
|
|
+ private func setupSubviews(with radius: CGFloat) {
|
|
|
+ addSubview(imageView)
|
|
|
+ imageView.layer.cornerRadius = radius
|
|
|
+ imageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
|
+ imageView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
|
|
+ imageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
|
|
+
|
|
|
addSubview(label)
|
|
|
- label.translatesAutoresizingMaskIntoConstraints = false
|
|
|
label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
|
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
|
|
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
|
|
+
|
|
|
+ addSubview(verifiedView)
|
|
|
+ let imgViewConstraints = [verifiedView.constraintAlignBottomTo(self, paddingBottom: -verificationViewPadding),
|
|
|
+ verifiedView.constraintAlignTrailingTo(self, paddingTrailing: -verificationViewPadding),
|
|
|
+ verifiedView.constraintAlignTopTo(self, paddingTop: radius + verificationViewPadding),
|
|
|
+ verifiedView.constraintAlignLeadingTo(self, paddingLeading: radius + verificationViewPadding)]
|
|
|
+ addConstraints(imgViewConstraints)
|
|
|
}
|
|
|
|
|
|
required init?(coder _: NSCoder) {
|
|
@@ -50,13 +80,15 @@ class InitialsBadge: UIView {
|
|
|
|
|
|
func setImage(_ image: UIImage) {
|
|
|
if let resizedImg = image.resizeImage(targetSize: CGSize(width: self.frame.width, height: self.frame.height)) {
|
|
|
- let attachment = NSTextAttachment()
|
|
|
- attachment.image = resizedImg
|
|
|
- label.attributedText = NSAttributedString(attachment: attachment)
|
|
|
+ self.imageView.image = resizedImg
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func setColor(_ color: UIColor) {
|
|
|
backgroundColor = color
|
|
|
}
|
|
|
+
|
|
|
+ func setVerified(_ verified: Bool) {
|
|
|
+ verifiedView.isHidden = !verified
|
|
|
+ }
|
|
|
}
|