Don_Mag

Untitled

Aug 11th, 2020
1,920
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TextFieldWTitle: UITextField {
  2.     var titleText: String = "" {
  3.         didSet {
  4.             label.text = titleText
  5.         }
  6.     }
  7.     var label = UILabel()
  8.    
  9.     override init(frame: CGRect) {
  10.         super.init(frame: frame)
  11.         self.genericConfigure()
  12.     }
  13.    
  14.     init() {
  15.         super.init(frame: CGRect.zero)
  16.         self.genericConfigure()
  17.     }
  18.    
  19.     required init?(coder: NSCoder) {
  20.         super.init(coder: coder)
  21.         self.genericConfigure()
  22.     }
  23.    
  24.     func configure(text: String,
  25.                    placeholder: String = "") {
  26.         self.text = text
  27.         self.placeholder = placeholder
  28.         if #available(iOS 13.0, *) {
  29.             self.applyStyle13()
  30.         } else {
  31.             self.applyStylePre13()
  32.         }
  33.     }
  34.    
  35.     func genericConfigure() {
  36.         self.borderStyle = .none
  37.        
  38.         self.layer.borderWidth = 1.0
  39.         self.layer.masksToBounds = false // true
  40.         self.layer.cornerRadius = 4.0
  41.        
  42.         self.textAlignment = .left
  43.        
  44.         label.text = "First name"
  45.         label.translatesAutoresizingMaskIntoConstraints = false
  46.         addSubview(label)
  47.         NSLayoutConstraint.activate([
  48.             label.leftAnchor.constraint(equalTo: leftAnchor),
  49.             label.bottomAnchor.constraint(equalTo: topAnchor),
  50.             label.heightAnchor.constraint(equalToConstant: 20.0)
  51.         ])
  52.     }
  53.    
  54.     @available(iOS 13.0, *)
  55.     func applyStyle13() {
  56.         self.tintColor = UIColor.label
  57.         self.backgroundColor = UIColor.white
  58.         self.textColor = UIColor.label
  59.         self.attributedPlaceholder = NSAttributedString(
  60.             string: self.placeholder ?? "",
  61.             attributes: [NSAttributedString.Key.foregroundColor: UIColor.tertiaryLabel])
  62.     }
  63.    
  64.     func applyStylePre13() {
  65.         self.tintColor = .yellow
  66.         self.backgroundColor = UIColor.white
  67.         self.textColor = .yellow
  68.         self.attributedPlaceholder = NSAttributedString(
  69.             string: self.placeholder ?? "",
  70.             attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])
  71.     }
  72.    
  73. }
  74.  
  75.  
  76. class TitleFieldViewController : UIViewController {
  77.     override func loadView() {
  78.         let view = UIView()
  79.        
  80.         view.backgroundColor = .white
  81.        
  82.         let stack = UIStackView()
  83.         //stack.frame = CGRect(x: 150, y: 200, width: 200, height: 200)
  84.         stack.distribution = .fill
  85.         stack.axis = .vertical
  86.        
  87.         let t1 = TextFieldWTitle()
  88.         t1.configure(text: "texta", placeholder: "placeholder")
  89.         let t2 = TextFieldWTitle()
  90.         t2.configure(text: "textb", placeholder: "placeholder")
  91.        
  92.         t2.titleText = "Last name"
  93.        
  94.         stack.addArrangedSubview(t1)
  95.         stack.addArrangedSubview(t2)
  96.        
  97.         view.addSubview(stack)
  98.        
  99.         stack.translatesAutoresizingMaskIntoConstraints = false
  100.         NSLayoutConstraint.activate([
  101.             stack.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
  102.             stack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100),
  103.             stack.widthAnchor.constraint(equalToConstant: 200),
  104.         ])
  105.        
  106.         self.view = view
  107.  
  108.         // run first *without* spacing
  109.         // then run *with* spacing
  110.         //stack.spacing = 40
  111.     }
  112. }
  113.  
  114. // Present the view controller in the Live View window
  115. PlaygroundPage.current.liveView = TitleFieldViewController()
  116.  
RAW Paste Data