diff --git a/README.md b/README.md index d824598..26d4d1a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example/Rakefile b/example/Rakefile index 129a145..914dbe6 100644 --- a/example/Rakefile +++ b/example/Rakefile @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- $:.unshift("/Library/RubyMotion/lib") -require 'motion/project' +require 'motion/project/template/ios' require 'bundler' Bundler.setup diff --git a/example/app/timer_controller.rb b/example/app/timer_controller.rb index 3282c14..bf67f34 100644 --- a/example/app/timer_controller.rb +++ b/example/app/timer_controller.rb @@ -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 diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index f3ccf25..c61f112 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -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 = [] @@ -21,18 +36,33 @@ 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 @@ -40,11 +70,11 @@ def strain 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)