Module: Walt::Font

Defined in:
lib/walt/support/font.rb

Class Method Summary collapse

Class Method Details

.attributes(params = {}) ⇒ Object

Create font attributes with keys: {

font: String or Hash (see Font.make),
color: String,
shadow_color: String,
shadow_offset: {
  x: Integer,
  y: Integer
}

}



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/walt/support/font.rb', line 71

def attributes(params = {})
  _attributes = {}

  _attributes[UITextAttributeFont] = Font.make(params[:font]) if params[:font]
  _attributes[UITextAttributeTextColor] = params[:color].to_color if params[:color]
  _attributes[UITextAttributeTextShadowColor] = params[:shadow_color].to_color if params[:shadow_color]
  _attributes[UITextAttributeTextShadowOffset] = begin
    x = params[:shadow_offset][:x]
    y = params[:shadow_offset][:y]
    offset = UIOffsetMake(x,y)
    NSValue.valueWithUIOffset(offset)
  end if params[:shadow_offset]

  _attributes
end

.bold(size) ⇒ Object



5
6
7
# File 'lib/walt/support/font.rb', line 5

def bold(size)
  Font.make(:bold, size)
end

.italic(size) ⇒ Object



13
14
15
# File 'lib/walt/support/font.rb', line 13

def italic(size)
  Font.make(:italic, size)
end

.make(params = {}, *args) ⇒ Object

Create font with params: String => The name of the font Hash =>

size: Integer,
name: String (see above)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/walt/support/font.rb', line 23

def make(params = {}, *args)
  if params.is_a?(UIFont)
    return params
  end

  _font = nil
  if params.is_a?(NSString)
    params = {name: params}
  end
  if args && args[0]
    params.merge!({size: args[0]})
  end
  params[:size] ||= UIFont.systemFontSize
  if [:system, :bold, :italic].member?(params[:name])
    case params[:name]
    when :system
      _font = UIFont.systemFontOfSize(params[:size].to_f)
    when :bold
      _font = UIFont.boldSystemFontOfSize(params[:size].to_f)
    when :italic
      _font = UIFont.italicSystemFontOfSize(params[:size].to_f)
    end
  end

  if !_font
    begin
      _font = UIFont.fontWithName(params[:name], size: params[:size])
    rescue
    end
  end

  if !_font
    raise "Invalid font for parameters: #{params.inspect} args #{args.inspect}"
  end

  _font
end

.system(size) ⇒ Object



9
10
11
# File 'lib/walt/support/font.rb', line 9

def system(size)
  Font.make(:system, size)
end