Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ Motion::Layout.new do |layout|
layout.horizontal "|-margin-[action]-margin-|"
end
```
### Layout Options

Check Motion::Layout::OPTIONS for available options. The default layout options is 0, which is same as [NSLayoutConstraint constraintsWithVisualFormat:options:mertics:views:]. Check [document](https://developer.apple.com/library/ios/DOCUMENTATION/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html#//apple_ref/occ/clm/NSLayoutConstraint/constraintsWithVisualFormat:options:metrics:views:) for meaning of each options.

``` ruby
Motion::Layout.new do |layout|
...
layout.vertical "[version]-|"
layout.horizontal "[version]-5-[version_number]-|", :baseline
end
```

## TODO

Expand Down
2 changes: 1 addition & 1 deletion example/Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'motion/project/template/ios'

require 'bundler'
Bundler.setup
Expand Down
18 changes: 17 additions & 1 deletion example/app/timer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,29 @@ def viewDidLoad
@action.setTitle('Stop', forState:UIControlStateSelected)
@action.addTarget(self, action:'actionTapped', forControlEvents:UIControlEventTouchUpInside)

@version = UILabel.new
@version.font = UIFont.systemFontOfSize(10)
@version.text = 'version'
@version.textAlignment = UITextAlignmentRight
@version.textColor = UIColor.whiteColor
@version.backgroundColor = UIColor.clearColor

@versionNumber = UILabel.new
@versionNumber.font = UIFont.systemFontOfSize(19)
@versionNumber.text = '0.1.0'
@versionNumber.textAlignment = UITextAlignmentRight
@versionNumber.textColor = UIColor.whiteColor
@versionNumber.backgroundColor = UIColor.clearColor

Motion::Layout.new do |layout|
layout.view view
layout.subviews "state" => @state, "action" => @action
layout.subviews "state" => @state, "action" => @action, "version" => @version, "version_number" => @versionNumber
layout.metrics "top" => 200, "margin" => 20, "height" => 40
layout.vertical "|-top-[state(==height)]-margin-[action(==height)]"
layout.vertical "[version]|"
layout.horizontal "|-margin-[state]-margin-|"
layout.horizontal "|-margin-[action]-margin-|"
layout.horizontal "[version]-5-[version_number]|", :baseline
end
end

Expand Down
46 changes: 38 additions & 8 deletions lib/motion-layout/layout.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
module Motion
class Layout
OPTIONS = {
none: 0,
left: NSLayoutFormatAlignAllLeft,
right: NSLayoutFormatAlignAllRight,
top: NSLayoutFormatAlignAllTop,
bottom: NSLayoutFormatAlignAllBottom,
leading: NSLayoutFormatAlignAllLeading,
trailing: NSLayoutFormatAlignAllTrailing,
centerx: NSLayoutFormatAlignAllCenterX,
centery: NSLayoutFormatAlignAllCenterY,
baseline: NSLayoutFormatAlignAllBaseline,
leading_to_trailing: NSLayoutFormatDirectionLeadingToTrailing,
left_to_right: NSLayoutFormatDirectionLeftToRight,
right_to_left: NSLayoutFormatDirectionRightToLeft
}
def initialize(&block)
@verticals = []
@horizontals = []
Expand All @@ -21,30 +36,45 @@ def view(view)
@view = view
end

def horizontal(horizontal)
@horizontals << horizontal
def horizontal(horizontal, *options)
options = [:none] if options.empty?
@horizontals << [horizontal, resolve_options(options)]
end

def vertical(vertical)
@verticals << vertical
def vertical(vertical, *options)
options = [:none] if options.empty?
@verticals << [vertical, resolve_options(options)]
end

private

def resolve_options(opt)
opt.inject(0) do |m,x|
if x.kind_of?(Numeric)
m | x.to_i
elsif o = OPTIONS[x.to_s.downcase.to_sym]
m | o
else
raise "invalid opt: #{x.to_s.downcase}"
end
end
end

def strain
@subviews.values.each do |subview|
next if @view == subview # you wouldn't believe the mess it would create
subview.translatesAutoresizingMaskIntoConstraints = false
@view.addSubview(subview) unless subview.superview
end

views = @subviews.merge("superview" => @view)

constraints = []
constraints += @verticals.map do |vertical|
NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:NSLayoutFormatAlignAllCenterX, metrics:@metrics, views:views)
constraints += @verticals.map do |vertical, options|
NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:options, metrics:@metrics, views:views)
end
constraints += @horizontals.map do |horizontal|
NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:NSLayoutFormatAlignAllCenterY, metrics:@metrics, views:views)
constraints += @horizontals.map do |horizontal, options|
NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:options, metrics:@metrics, views:views)
end

@view.addConstraints(constraints.flatten)
Expand Down