lib/lhj/command/view.rb in lhj-tools-0.1.2 vs lib/lhj/command/view.rb in lhj-tools-0.1.3
- old
+ new
@@ -1,35 +1,37 @@
module Lhj
class Command
class View < Command
- self.summary = '生成源码'
+ self.summary = '生成iOS组件源码'
+ self.description = '使用`lhj view --type=UILabel --name=titleLabel`'
def initialize(argv)
@name = argv.option('name', 'titleLabel')
@type = argv.option('type', 'UILabel')
super
end
def names
- @name.split(",").map(&:strip)
+ @name.split(',').map(&:strip)
end
def type
@ele_type ||= begin
- if @type =~ /image/i
+ case @type
+ when /image/i
'UIImageView'
- elsif @type =~ /stack/i
+ when /stack/i
'UIStackView'
- elsif @type =~ /label/i
+ when /label/i
'UILabel'
- elsif @type =~ /table/i
+ when /table/i
'UITableView'
- elsif @type =~ /text/i
+ when /text/i
'UITextField'
- elsif @type =~ /button/i
+ when /button/i
'UIButton'
- elsif @type =~ /view/i
+ when /view/i
'UIView'
else
@type
end
end
@@ -45,70 +47,72 @@
print_value
end
def print_declare
names.each do |name|
- puts "///"
+ puts '///'
puts "@property (nonatomic, strong) #{type} *#{name};"
end
end
def print_instance
names.each do |name|
puts "-(#{type} *)#{name}"
- puts "{"
+ puts '{'
puts " if(!_#{name}){"
print_alloc(name)
puts " _#{name}.translatesAutoresizingMaskIntoConstraints = NO;"
print_property(name)
- puts " }"
+ puts ' }'
puts " return _#{name};"
- puts "}"
+ puts '}'
puts "\n"
end
end
def print_alloc(name)
- if type.eql?('UIImageView')
+ case type
+ when 'UIImageView'
puts " _#{name} = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"xxxx\"]];"
- elsif type.eql?('UIButton')
+ when 'UIButton'
puts " _#{name} = [UIButton buttonWithType:UIButtonTypeCustom];"
- elsif type.eql?('UITableView')
+ when 'UITableView'
puts " _#{name} = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];"
else
puts " _#{name} = [[#{type} alloc] init];"
end
end
def print_property(name)
- if type.eql?('UILabel')
+ case type
+ when 'UILabel'
puts " _#{name}.textColor = kSetCOLOR(0x333333);"
puts " _#{name}.text = @\"xxxxxxxx\";"
puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
puts " _#{name}.textAlignment = NSTextAlignmentCenter;"
- elsif type.eql?('UIImageView')
+ when 'UIImageView'
puts " _#{name}.backgroundColor = kBackgroundColor;"
puts " _#{name}.contentMode = UIViewContentModeScaleAspectFit;"
puts " _#{name}.clipsToBounds = YES;"
puts " _#{name}.layer.cornerRadius = 6.0f;"
puts " _#{name}.layer.borderColor = kLineColor.CGColor;"
puts " _#{name}.layer.borderWidth = 0.5;"
- elsif type.eql?('UITextField')
+ when 'UITextField'
puts " _#{name}.textColor = kSetCOLOR(0x333333);"
puts " _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
- elsif type.eql?('UIView')
+ when 'UIView'
puts " _#{name}.backgroundColor = kBackgroundColor;"
- elsif type.eql?('UIStackView')
+ when 'UIStackView'
puts " _#{name}.axis = UILayoutConstraintAxisHorizontal;"
puts " _#{name}.distribution = UIStackViewDistributionFillEqually;"
- elsif type.eql?('UITableView')
+ when 'UITableView'
puts " _#{name}.backgroundColor = kBackgroundColor;"
puts " _#{name}.delegate = self;"
puts " _#{name}.delegate = self;"
puts " _#{name}.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
puts " _#{name}.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];"
puts " _#{name}.separatorStyle = UITableViewCellSeparatorStyleNone;"
- elsif type.eql?('UIButton')
+ when 'UIButton'
puts " _#{name}.backgroundColor = kBackgroundColor;"
puts " [_#{name} setTitle:@\"xxx\" forState:UIControlStateNormal];"
puts " [_#{name} setTitleColor:kSetCOLOR(0x999999) forState:UIControlStateNormal];"
puts " _#{name}.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightRegular];"
puts " [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateNormal];"