Advertisement
Guest User

Untitled

a guest
Nov 7th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. class MyView: UIView {
  2.  
  3.     let kLabelHorizontalInsets: CGFloat = 20.0
  4.     let kLabelVerticalInsets: CGFloat = 20.0
  5.    
  6.     var didSetupConstraints = false
  7.    
  8.     var titleLabel: UILabel = UILabel.newAutoLayoutView()
  9.     var bodyLabel: UILabel = UILabel.newAutoLayoutView()
  10.  
  11.     override init() {
  12.         super.init()
  13.         self.setupViews()
  14.     }
  15.    
  16.     override init(frame: CGRect) {
  17.         super.init(frame: frame)
  18.         self.setupViews()
  19.     }
  20.    
  21.     required init(coder aDecoder: NSCoder) {
  22.         super.init(coder: aDecoder)
  23.         self.setupViews()
  24.     }
  25.    
  26.     func setupViews() {
  27.  
  28.         self.backgroundColor = UIColor.redColor()
  29.         self.addSubview(self.getTitleLabel(title: "FooBar"))
  30.         self.addSubview(self.getBodyLabel(title: "Test"))
  31.        
  32.         self.updateFonts()
  33.     }
  34.    
  35.     override func updateConstraints() {
  36.         if !didSetupConstraints {
  37.            
  38.             self.titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: kLabelHorizontalInsets)
  39.             self.titleLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: kLabelVerticalInsets)
  40.  
  41.             self.bodyLabel.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.titleLabel, withOffset: 0)
  42.             self.bodyLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: kLabelHorizontalInsets)
  43.            
  44.             self.didSetupConstraints = true
  45.         }
  46.         super.updateConstraints()
  47.     }
  48.    
  49.     func getTitleLabel(#title: String) -> UILabel {
  50.         self.titleLabel = UILabel.newAutoLayoutView()
  51.         self.titleLabel.numberOfLines = 1
  52.         self.titleLabel.text = title
  53.         self.titleLabel.backgroundColor = UIColor.greenColor()
  54.        
  55.         return self.titleLabel
  56.     }
  57.    
  58.     func getBodyLabel(#title: String) -> UILabel {
  59.         self.bodyLabel = UILabel.newAutoLayoutView()
  60.         self.bodyLabel.numberOfLines = 0
  61.         self.bodyLabel.text = title
  62.         self.bodyLabel.backgroundColor = UIColor.yellowColor()
  63.        
  64.         return self.bodyLabel
  65.     }
  66.  
  67.     func updateFonts() {
  68.         self.titleLabel.font = UIFont.boldSystemFontOfSize(20)
  69.         self.bodyLabel.font = UIFont.systemFontOfSize(18)
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement