Skip to content

Instantly share code, notes, and snippets.

@giovani-pereira-ifood
Last active September 25, 2020 13:06
Show Gist options
  • Save giovani-pereira-ifood/2c295f840a74509820d5fdaaa81a8283 to your computer and use it in GitHub Desktop.
Save giovani-pereira-ifood/2c295f840a74509820d5fdaaa81a8283 to your computer and use it in GitHub Desktop.
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