Module: MotionAwesome

Included in:
Z
Defined in:
lib/utils/generator.rb,
lib/motion-awesome/awesome.rb,
lib/motion-awesome/version.rb

Defined Under Namespace

Classes: Generator, InvalidAwesomeIconError

Constant Summary collapse

VERSION =
'0.0.6'

Class Method Summary collapse

Class Method Details

.attributed_text(opts) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/motion-awesome/awesome.rb', line 37

def attributed_text( opts )
  awesome_attrs = MotionMap::Map[NSFontAttributeName, font(opts[:size])]
  awesome_attrs[NSForegroundColorAttributeName] = opts.color if opts.color?
  text          = hex_for_icon( opts.icon )
  text         += " " + opts.text if opts.text?
  range         = opts.text? ? (0..1) : (0..0)
  NSMutableAttributedString.alloc.initWithString( text, attributes: nil ).tap do |attrs|
    attrs.setAttributes( awesome_attrs, range:range )
  end
end

.button(icon, options = {}) {|comp| ... } ⇒ Object

Yields:

  • (comp)


6
7
8
9
10
11
12
# File 'lib/motion-awesome/awesome.rb', line 6

def button( icon, options={}, &block )
  opts = parse_options( icon, options )
  comp = UIButton.buttonWithType( map_types( opts.type? ? opts.type : nil ) )
  comp.setAttributedTitle( attributed_text(opts), forState: UIControlStateNormal )
  yield comp if block_given?
  comp
end

.button_typesObject



66
67
68
69
# File 'lib/motion-awesome/awesome.rb', line 66

def button_types
  @button_types ||=
    MotionMap::Map[custom: UIButtonTypeCustom, rounded: UIButtonTypeRoundedRect]
end

.font(size) ⇒ Object



23
24
25
# File 'lib/motion-awesome/awesome.rb', line 23

def font( size )
  UIFont.fontWithName( 'FontAwesome', size:size )
end

.hex_for_icon(icon) ⇒ Object



48
49
50
51
52
# File 'lib/motion-awesome/awesome.rb', line 48

def hex_for_icon( icon )
  index = plist["fa-#{icon}"]
  raise InvalidAwesomeIconError, "Unable to find icon representation for `#{icon.inspect}" unless index
  index.hex.chr(Encoding::UTF_8)
end

.label(icon, options = {}) {|comp| ... } ⇒ Object

Yields:

  • (comp)


14
15
16
17
18
19
20
21
# File 'lib/motion-awesome/awesome.rb', line 14

def label( icon, options={}, &block )
  opts = parse_options( icon, options )
  comp = UILabel.alloc.initWithFrame( [[0,0],[opts[:size],opts[:size]]] )
  comp.setAttributedText( attributed_text(opts) )
  comp.awesome_color = opts.color if opts.color?
  yield comp if block_given?
  comp
end

.map_types(type) ⇒ Object



54
55
56
# File 'lib/motion-awesome/awesome.rb', line 54

def map_types( type )
  button_types.get(type) {UIButtonTypeRoundedRect}
end

.parse_options(icon, opts) ⇒ Object



27
28
29
30
31
# File 'lib/motion-awesome/awesome.rb', line 27

def parse_options( icon, opts )
  options        =  MotionMap::Map[opts.merge( icon: xform_icon(icon) )]
  options[:size] = UIFont.systemFontSize unless options[:size]
  options
end

.plistObject



58
59
60
61
62
63
64
# File 'lib/motion-awesome/awesome.rb', line 58

def plist
  @plist ||= begin
    NSMutableDictionary.dictionaryWithContentsOfFile(
      NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent( "fontawesome.plist" )
    )
  end
end

.xform_icon(icon) ⇒ Object



33
34
35
# File 'lib/motion-awesome/awesome.rb', line 33

def xform_icon( icon )
  icon.to_s.gsub( /_/, '-')
end