Skip to content

Instantly share code, notes, and snippets.

@orkhanalizade
Created November 23, 2018 21:48
Show Gist options
  • Save orkhanalizade/747dc4fd1eb9f228ac964fb4048125dc to your computer and use it in GitHub Desktop.
Save orkhanalizade/747dc4fd1eb9f228ac964fb4048125dc to your computer and use it in GitHub Desktop.
import UIKit
class MyView: UIView {
var label: UILabel!
var text: String? {
didSet {
label.text = text
}
}
var cornerRadius: CGFloat = 0.0 {
didSet {
self.layer.cornerRadius = self.cornerRadius
self.layer.masksToBounds = true
}
}
var textColor: UIColor = UIColor.black {
didSet {
label.textColor = textColor
}
}
var isTextCentered: Bool = false {
didSet {
self.label.textAlignment = isTextCentered ? .center : .left
}
}
init() {
super.init(frame: CGRect(x: 8.0, y: 60.0, width: UIScreen.main.bounds.width - 16.0, height: 60.0))
initialize()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func initialize() {
// self.translatesAutoresizingMaskIntoConstraints = false
// self.heightAnchor.constraint(greaterThanOrEqualToConstant: 60.0).isActive = true
// NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 60.0).isActive = true
label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
self.addSubview(label)
let constraints = [
NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 8.0),
NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 8.0),
NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: -8.0),
NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -8.0)
]
NSLayoutConstraint.activate(constraints)
self.addConstraints(constraints)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment