Class: Pod::Command::Bin::View

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/view.rb

Instance Method Summary collapse

Methods inherited from Pod::Command::Bin

#validate!

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ View

Returns a new instance of View.



7
8
9
10
11
# File 'lib/cocoapods-lhj-bin/command/bin/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/cocoapods-lhj-bin/command/bin/view.rb', line 13

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


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 71

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


49
50
51
52
53
54
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 49

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


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 56

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


122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 122

def print_layout
  names.each do |name|
    puts "[contentView addSubview:self.#{name}];"
    puts "[self.#{name}.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor constant:0].active = YES;"
    puts "[self.#{name}.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor constant:0].active = YES;"
    puts "[self.#{name}.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = YES;"
    puts "[self.#{name}.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = YES;"
    puts "[self.#{name}.widthAnchor constraintEqualToConstant:80].active = YES;"
    puts "[self.#{name}.heightAnchor constraintEqualToConstant:80].active = YES;"
    puts "\n\n"
  end
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 83

def print_property(name)
  if type.eql?('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')
    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')
    puts "        _#{name}.textColor = kSetCOLOR(0x333333);"
    puts "        _#{name}.font = [UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular];"
  elsif type.eql?('UIView')
    puts "        _#{name}.backgroundColor = kBackgroundColor;"
  elsif type.eql?('UIStackView')
    puts "        _#{name}.axis = UILayoutConstraintAxisHorizontal;"
    puts "        _#{name}.distribution = UIStackViewDistributionFillEqually;"
  elsif type.eql?('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')
    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];"
    puts "        [_#{name} setImage:[UIImage imageNamed:@\"xx\"] forState:UIControlStateSelected];"
    puts "        [_#{name} addTarget:self action:@selector(actionHandler:) forControlEvents:UIControlEventTouchUpInside];"
  end
end


135
136
137
138
139
140
141
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 135

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

#runObject



39
40
41
42
43
44
45
46
47
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 39

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
# File 'lib/cocoapods-lhj-bin/command/bin/view.rb', line 17

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