seanho / SimpleView

RubyMotion DSL for UIKit
MIT License
103 stars 10 forks source link

SimpleView

RubyMotion DSL for UIKit.

Change log

Version 0.6.x

Breaking changes:

Block scoping is removed, i.e. no need to pass in locals.

Previous

class ViewController
  def viewDidLoad
    setup view, controller: self do
      table_view delegate: controller, dataSource: controller
    end
  end
end

Now

class ViewController
  def viewDidLoad
    setup view do
      table_view delegate: self, dataSource: self
    end
  end
end

add is renamed to add_view for clarity.

Installation

Add the gem to your Gemfile

gem 'simple-view'

Then bundle install

Require SimpleView in Rakefile

$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'simple-view'

Motion::Project::App.setup do |app|
  ...
end

Usage

class YourViewController < UIViewController
  include SimpleView::Layout

  def viewDidLoad
    setup content_view do
      label top:0, left: 0, width: 200, height: 20, text: "Choose your lucky word", text_color: "#eee"
      image_view top: 50, left: 50, right: 50, image: "sample.jpg"
      toolbar left: 0, right: 0, bottom: 0
    end
  end
end

Use block for more control over the view instance

def viewDidLoad
  setup view do
    button tint_color: '#f00' do |button|
      button.setTitle "Submit" forState: UIControlStateNormal
    end
  end
end

UIKit support

SimpleView provides shorthand methods for most UIKit classes

def viewDidLoad
  setup view do
    label text: 'Hi there!' # shorthand
    add_view UILabel, text: 'Hi there!' # same as above
  end
end

Custom view support

SimpleView can work with any custom views and controls

setup view do
  add_view CustomViewClass, name: "custom_view"...
end

Style Template

Define default style and apply to UI classes automatically

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    SimpleView::Styles.define UILabel,
      font: "italic 13",
      text_color: "#999"
  end
end

class ViewController
  include SimpleView::Layout

  def viewDidLoad
    setup view do
      label text: "Label with default style!"
    end
  end
end

Define custom styles and mix with default style

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    SimpleView::Styles.define UILabel,
      font: "13"

    SimpleView::Styles.define :darkcolor,
      text_color: "#999"

    SimpleView::Styles.define :title,
      font: "bold 15"
  end
end

class ViewController
  include SimpleView::Layout

  def viewDidLoad
    setup view do
      label styles: :darkcolor, text: "13 font with dark color"
      label styles: [:darkcolor, :title], text: "bold 15 font with dark color"
    end
  end
end

View positioning and sizing

Position the view without doing a lot of calculation

setup view do
  toolbar bottom: 10, left: 10, right: 10
end
setup view do
  table_view width: 100.percent, height: 100.percent
end

View tagging with string

Tag view with name string and find them with ease

def viewDidLoad
  setup view do
    label name: 'desc'
    label name: 'price'
  end
end

def someOtherMethod
  price_label = view.find('price')
  desc_label = price_label.sibling('desc')
end