HGCodeBaseUI

Design for Writing UI with Code


License
MIT
Install
pod try HGCodeBaseUI

Documentation

ViewBuilderSwift

Cocoapods Cocoapods platforms Language License

This will make it easier for you to write UI without Interface Builder by taking advantage of two features:

Features

Design for Writing UI with Code

protocol UI

UI protocol is conformed by a class that replaces the role of the Interface Builder.

protocol UIOwner

UIOwner protocol represents the type that owns the UI.

By default, UIViewController and UIView conforms UIOwner.

Example

final class SomeViewController: UIViewController {
  
  private var viewUI: SomeUI!
  
  override func loadView() {
    viewUI = .init(owner: self)
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do something with `viewUI.label`...
  }
}
final class SomeUI: UI {
  
  // Recommended to set `owner` as an unowned variable.
  unowned var owner: SomeViewController
  
  // Recommended to define UI components using `lazy` keyword.
  lazy var label = UILabel()
  
  init(owner: SomeViewController) {
    self.owner = owner
    
    owner.view = .init()
    view.backgroundColor = .white
    
    // Configuring label...
  }
}

Note | You must write owner.view = .init() at top of init(owner: Owner) when you initialize UI at loadView() method. Otherwise the app will crash because any view controller's view property is assigned after loadView() method is called.

Property Setting with Method Chaining

This code block...

let label = UILabel()
label.text = "Hello World!"
label.textColor = .blue
label.font = .systemFont(ofSize: 15)
label.numberOfLines = 1
label.lineBreakMode = .byWordWrapping

can be substituted by the following code block.

let label = UILabel().builder
  .text("Hello World!")
  .textColor(.blue)
  .font(.systemFont(ofSize: 15))
  .numberOfLines(1)
  .lineBreakMode(.byWordWrapping)
  .build()

You can use a wrapper for an existing API, but you can also use several helper methods.

By using helpers, you can replace the code above with the code below.

let label = UILabel().builder
  .text(.plain("Hello World"))
  .textStyle(.color(.blue))
  .textStyle(.font(.systemFont(ofSize: 15)))
  .numberOfLines(1)
  .lineBreakMode(.byWordWrapping)
  .build()

You can also use builders after the object is initialized.

let label = UILabel()
label.builder
  .text(.plain("Hello World"))
  .textStyle(.color(.blue))
  .textStyle(.font(.systemFont(ofSize: 15)))
  .numberOfLines(1)
  .lineBreakMode(.byWordWrapping)

In addition to text settings, it also provides helper methods for setting coordinates(CGPoint), size(CGSize), rectangle(CGRect), edge insets(UIEdgeInsets), and more.

Dependencies

Requrements

  • Swift 4.2 / iOS

Installation

ViewBuilderSwift supports Cocoapods only.

Podfile

pod 'ViewBuilderSwift'
pod install

License

ViewBuilderSwift is under MIT license. See the LICENSE for more info.