Last active
September 25, 2020 13:06
-
-
Save giovani-pereira-ifood/2c295f840a74509820d5fdaaa81a8283 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IconView: UIView { | |
var icon: Icon | |
var color: UIColor | |
private let label: UILabel = { | |
let font = UIFont.iconFont(ofSize: 500) | |
let label = UILabel() | |
label.font = font | |
label.adjustsFontSizeToFitWidth = true | |
label.minimumScaleFactor = 0.01 | |
label.textAlignment = .center | |
label.sizeToFit() | |
// Prevents resizing by text accessibity features | |
label.adjustsFontForContentSizeCategory = false | |
return label | |
}() | |
init(icon: Icon, color: UIColor) { | |
self.icon = icon | |
self.color = color | |
super.init(frame: .zero) | |
setup() | |
} | |
private func setup() { | |
addSubview(label) | |
constrainLabel() | |
label.text = icon.rawValue | |
label.textColor = color | |
} | |
private func constrainLabel() { | |
label.translatesAutoresizingMaskIntoConstraints = false | |
self.translatesAutoresizingMaskIntoConstraints = false | |
let centerX = self.centerXAnchor.constraint(equalTo: label.centerXAnchor) | |
let centerY = self.centerYAnchor.constraint(equalTo: label.centerYAnchor) | |
let width = self.widthAnchor.constraint(equalTo: label.widthAnchor, multiplier: proportionFontToWrapper) | |
let height = self.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: proportionFontToWrapper) | |
NSLayoutConstraint.activate([centerX, centerY, width, height]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment