Class: Lhj::Command::View

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/code/view.rb

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #handle, #stop

Constructor Details

#initialize(argv) ⇒ View

Returns a new instance of View.



7
8
9
10
11
# File 'lib/lhj/command/code/view.rb', line 7

def initialize(argv)
  @name = argv.option('name', 'titleLabel')
  @type = argv.option('type', 'UILabel')
  super
end

Instance Method Details

#namesObject



13
14
15
# File 'lib/lhj/command/code/view.rb', line 13

def names
  @name.split(',').map(&:strip)
end


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lhj/command/code/view.rb', line 88

def print_alloc(name)
  case type
  when 'UIImageView'
    puts_i "        _#{name} = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"xxxx\"]];"
  when 'UIButton'
    puts_i "        _#{name} = [UIButton buttonWithType:UIButtonTypeCustom];"
  when 'UITableView'
    puts_i "        _#{name} = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];"
  else
    puts_i "        _#{name} = [[#{type} alloc] init];"
  end
end


66
67
68
69
70
71
# File 'lib/lhj/command/code/view.rb', line 66

def print_declare
  names.each do |name|
    puts_d '///'
    puts_d "@property (nonatomic, strong) #{type} *#{name};"
  end
end


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lhj/command/code/view.rb', line 73

def print_instance
  names.each do |name|
    puts_i "-(#{type} *)#{name}"
    puts_i '{'
    puts_i "    if(!_#{name}){"
    print_alloc(name)
    puts_i "        _#{name}.translatesAutoresizingMaskIntoConstraints = NO;"
    print_property(name)
    puts_i '    }'
    puts_i "    return _#{name};"
    puts_i '}'
    puts_i "\n"
  end
end


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lhj/command/code/view.rb', line 141

def print_layout
  names.each do |name|
    puts_l "[contentView addSubview:self.#{name}];"
    puts_l "[self.#{name}.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor constant:0].active = YES;"
    puts_l "[self.#{name}.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor constant:0].active = YES;"
    puts_l "[self.#{name}.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = YES;"
    puts_l "[self.#{name}.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = YES;"
    puts_l "[self.#{name}.widthAnchor constraintEqualToConstant:80].active = YES;"
    puts_l "[self.#{name}.heightAnchor constraintEqualToConstant:80].active = YES;"
    if type.eql?('UILabel')
      puts_l "[self.#{name} setContentHuggingPriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
      puts_l "[self.#{name} setContentCompressionResistancePriority:300 forAxis:UILayoutConstraintAxisHorizontal];"
    end
    puts_l "\n\n"
  end
end


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lhj/command/code/view.rb', line 101

def print_property(name)
  case type
  when 'UILabel'
    puts_i "        _#{name}.textColor = kSetCOLOR(0x333333);"
    puts_i "        _#{name}.text = @\"xxxxxxxx\";"
    puts_i "        _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
    puts_i "        _#{name}.textAlignment = NSTextAlignmentCenter;"
  when 'UIImageView'
    puts_i "        _#{name}.backgroundColor = kBackgroundColor;"
    puts_i "        _#{name}.contentMode = UIViewContentModeScaleAspectFit;"
    puts_i "        _#{name}.clipsToBounds = YES;"
    puts_i "        _#{name}.layer.cornerRadius = 6.0f;"
    puts_i "        _#{name}.layer.borderColor = kLineColor.CGColor;"
    puts_i "        _#{name}.layer.borderWidth = 0.5;"
  when 'UITextField'
    puts_i "        _#{name}.textColor = kSetCOLOR(0x333333);"
    puts_i "        _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
  when 'UIView'
    puts_i "        _#{name}.backgroundColor = kBackgroundColor;"
  when 'UIStackView'
    puts_i "        _#{name}.axis = UILayoutConstraintAxisHorizontal;"
    puts_i "        _#{name}.distribution = UIStackViewDistributionFillEqually;"
  when 'UITableView'
    puts_i "        _#{name}.backgroundColor = kBackgroundColor;"
    puts_i "        _#{name}.delegate = self;"
    puts_i "        _#{name}.delegate = self;"
    puts_i "        _#{name}.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
    puts_i "        _#{name}.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
    puts_i "        _#{name}.separatorStyle = UITableViewCellSeparatorStyleNone;"
  when 'UIButton'
    puts_i "        _#{name}.backgroundColor = kBackgroundColor;"
    puts_i "        [_#{name} setTitle:@\"xxx\" forState:UIControlStateNormal];"
    puts_i "        [_#{name} setTitleColor:kSetCOLOR(0x999999) forState:UIControlStateNormal];"
    puts_i "        _#{name}.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];"
    puts_i "        [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateNormal];"
    puts_i "        [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateSelected];"
    puts_i "        [_#{name} addTarget:self action:@selector(actionHandler:) forControlEvents:UIControlEventTouchUpInside];"
  end
end


158
159
160
161
162
163
164
# File 'lib/lhj/command/code/view.rb', line 158

def print_value
  names.each do |name|
    if type.eql?('UILabel')
      puts_v "self.#{name}.text = @\"xxxxx\";"
    end
  end
end

#puts_d(str) ⇒ Object



50
51
52
# File 'lib/lhj/command/code/view.rb', line 50

def puts_d(str)
  puts str.blue
end

#puts_i(str) ⇒ Object



54
55
56
# File 'lib/lhj/command/code/view.rb', line 54

def puts_i(str)
  puts str.blue
end

#puts_l(str) ⇒ Object



58
59
60
# File 'lib/lhj/command/code/view.rb', line 58

def puts_l(str)
  puts str.blue
end

#puts_v(str) ⇒ Object



62
63
64
# File 'lib/lhj/command/code/view.rb', line 62

def puts_v(str)
  puts str.blue
end

#runObject



40
41
42
43
44
45
46
47
48
# File 'lib/lhj/command/code/view.rb', line 40

def run
  print_declare
  puts "\n\n"
  print_instance
  puts "\n\n"
  print_layout
  puts "\n\n"
  print_value
end

#typeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lhj/command/code/view.rb', line 17

def type
  @ele_type ||= begin
                  case @type
                  when /image/i
                    'UIImageView'
                  when /stack/i
                    'UIStackView'
                  when /label/i
                    'UILabel'
                  when /table/i
                    'UITableView'
                  when /text/i
                    'UITextField'
                  when /button/i
                    'UIButton'
                  when /view/i
                    'UIView'
                  else
                    @type
                  end
                end
end