# -*- coding:utf-8 -*-
import re
def autowrite(file_path):
with open(file_path, 'r') as f:
result = f.read()
mylist = re.findall(r"(strong|assign|weak|atomic|copy)[ ]*\)(.*?)\;", result)
writeList = []
configList = []
for t in mylist:
s = t[1]
l = re.split(" |\*", s)
n = [x for x in l if x.strip()!='']
configList.append(n)
# 重复的去除
if result.find('(%s *)%s' % (n[0], n[-1])) != -1:
continue
if n[0] == 'UITableView':
writeList.append(generateTableView1(n[0], n[-1]))
elif n[0] == 'UIButton':
writeList.append(generateButton1(n[0], n[-1]))
elif n[0] == 'UILabel':
writeList.append(generateLabel1(n[0], n[-1]))
elif n[0] == 'UIImageView':
writeList.append(generateImageView1(n[0], n[-1]))
elif n[0] == 'UIView':
writeList.append(generateView(n[0], n[-1]))
elif n[0] == 'UITextField':
writeList.append(generateTextField(n[0], n[-1]))
elif n[0] == 'UITextView':
writeList.append(generateTextView(n[0], n[-1]))
elif ignore_fields(n[0]):
writeList.append(generateDefaultClass(n[0], n[-1]))
# 写到文件
# 写config code
configCodeList = ['''\n- (void)configUI{\n\t''']
temp1 = []
temp2 = []
for item in configList:
temp1.append(generateConfigUI(item[1]))
temp2.append(generateConfigUILayout1(item[1]))
configCodeList.extend(temp1)
configCodeList.extend(temp2)
configCodeList.append('''\n}\n''')
configString = ''.join(configCodeList)
uiCodes = ''
if len(configString) >0:
uiCodes = configString
#写getter
writeString = ''.join(writeList)
if len(writeString) > 0:
with open(file_path, 'w') as w:
myend = re.findall(r"@end", result)
pox = result.rfind("@end")
if len(myend) >= 2:
w.write(result[:pox] + '\n#pragma mark - auto generate\n' + writeString + '\n' + '\n#pragma mark - config UI data\n' + uiCodes +'\n' + result[pox:])
else:
w.write(result + '\n#pragma mark - setter && getter\n' + writeString + '\n' + '\n#pragma mark - config UI data\n' + uiCodes +'\n' + '@end')
# 需要忽略的 对象
def ignore_fields(field):
if field != 'BOOL' and field != 'float' and field != 'NSUInteger' and field != 'id' and field != 'SEL' and field != 'NSDate':
return True
else:
return False
# 生成 tableView
def generateTableView(className='UITableView', classValue='tableView'):
return ('''\n- (%s *)%s {\n if (!_%s) {\n _%s = [[%s alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];\n _%s.tableFooterView = [UIView new];\n _%s.backgroundColor = COLOR_VIEW_BACKGROUND;\n _%s.dataSource = self;\n _%s.delegate = self;\n }\n return _%s;\n}\n''' % (
className, classValue, classValue, classValue, className, classValue, classValue, classValue, classValue, classValue))
# 生成 tableView1
def generateTableView1(className='UITableView', classValue='tableView'):
return ('''\n- (%s *)%s {
if (!_%s) {
_%s = [[%s alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_%s.tableFooterView = [UIView new];
_%s.tableHeaderView = [UIView new];
[_%s registerClass:[<#xx#> class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([<#xx#> class])];
[_%s registerClass:[<#xx#> class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([<#xx#> class])];
[_%s registerClass:[<#xx#> class] forCellReuseIdentifier:NSStringFromClass([<#xx#> class])];
_%s.backgroundColor = [UIColor colorWithHexString:@"<#9b9b9b#>"];
_%s.dataSource = self;
_%s.delegate = self;
}
return _%s;
\n}''' % (className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue,classValue,classValue,classValue,classValue))
# 生成 button
def generateButton(className='UIButton', classValue='defaultButton'):
return ('\n- (%s *)%s {\n\tif (!_%s) {\n\t\t_%s = [%s buttonWithType:UIButtonTypeCustom];\n\t\t[_%s.titleLabel setFont:[UIFont systemFontOfSize:14]];\n\t\t[_%s setTitle:@"<#title#>" forState:UIControlStateNormal];\n\t\t_%s.layer.cornerRadius = 5.0f; \n\t\t_%s.layer.masksToBounds = YES;\n\t\t[_%s addTarget:self action:@selector(%sAction:) forControlEvents:UIControlEventTouchUpInside];\n\t}\n\treturn _%s;\n}\n' % (
className, classValue, classValue, classValue, className, classValue, classValue, classValue, classValue, classValue, classValue, classValue))
# 生成 button1
def generateButton1(className='UIButton', classValue='defaultButton'):
return ('''\n- (%s *)%s {
if (!_%s) {
_%s = [%s buttonWithType:UIButtonTypeCustom];
[_%s.titleLabel setFont:[UIFont systemFontOfSize:14]]; x
[_%s setTitle:@"<#title#>" forState:UIControlStateNormal];
[_%s setImage:[UIImage imageNamed:@"<#xxx#>"] forState:UIControlStateNormal];
[_%s setBackgroundImage:[UIImage imageNamed:@"<#xxx#>"] forState:UIControlStateNormal];
_%s.backgroundColor = [UIColor colorWithHexString:@"<#9b9b9b#>"];
[_%s setTitleColor:[UIColor colorWithHexString:@"<#9b9b9b#>"] forState:UIControlStateNormal];
_%s.layer.cornerRadius = 5.0f;
_%s.layer.masksToBounds = YES;
[_%s addTarget:self action:@selector(myBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _%s;
\n}''' % (className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue,classValue,classValue,classValue,classValue,classValue))
# 生成 label
def generateLabel(className='UILabel', classValue='defaultLabel'):
return ('''\n- (%s *)%s {\n\tif (!_%s) {\n\t\t_%s = [[%s alloc] init];\n\t\t[_%s setFont:[UIFont systemFontOfSize:12.0f]];\n\t\t[_%s setTextColor:[UIColor colorWithHexString:@"<#9b9b9b#>"]];\n\t\t_%s.textAlignment = NSTextAlignmentLeft;\n\t\t_%s.text = @"<#label#>";\n\t}\n\treturn _%s;\n}\n''' % (
className, classValue, classValue, classValue, className, classValue, classValue, classValue,classValue, classValue))
# 生成 label1
def generateLabel1(className='UILabel', classValue='defaultLabel'):
return ('''\n- (%s *)%s {
if (!_%s) {
_%s = [[%s alloc] initWithFrame:CGRectZero];
[_%s setFont:[UIFont systemFontOfSize:12.0f]];
[_%s setTextColor:[UIColor colorWithHexString:@"<#9b9b9b#>"]];
_%s.textAlignment = NSTextAlignmentLeft;
_%s.text = @"<#label#>";
_%s.backgroundColor = [UIColor colorWithHexString:@"<#9b9b9b#>"];
}
return _%s;
\n}''' % (className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue,classValue))
#生成 UIImageView
def generateImageView(className='UIImageView', classValue='defaultImageView'):
return ('''\n- (%s *)%s {\n\tif (!_%s) {\n\t\t_%s = [[%s alloc] init];\n\t\t_%s.image = [UIImage imageNamed:@"<#imgname#>"];\n\t\t_%s.clipsToBounds = YES; \n\t\t_%s.contentMode = UIViewContentModeScaleAspectFill;\n\t}\n\treturn _%s;\n}\n''' % (
className, classValue, classValue, classValue, className, classValue, classValue, classValue, classValue))
#生成 UIImageView1
def generateImageView1(className='UIImageView', classValue='defaultImageView'):
return ('''\n- (%s *)%s {
if (!_%s) {
_%s = [[%s alloc] init];
_%s.image = [UIImage imageNamed:@"<#imgname#>"];
_%s.clipsToBounds = YES;
_%s.contentMode = UIViewContentModeScaleAspectFill;
_%s.backgroundColor = [UIColor colorWithHexString:@"<#9b9b9b#>"];
}
return _%s;
\n}''' %(className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue))
#生成 UIView
def generateView(className='UIView', classValue='defaultImageView'):
return ('''\n-(%s *)%s{
if(!_%s){
_%s = [[UIView alloc] initWithFrame:CGRectZero];
_%s.clipsToBounds = YES;
_%s.layer.cornerRadius = 10.0f;
_%s.backgroundColor = [UIColor whiteColor];
}
return _%s;
\n}''' %(className,classValue,classValue,classValue,classValue,classValue,classValue,classValue))
#生成 UITextField
def generateTextField(className='UITextField', classValue='defaultImageView'):
return ('''\n-(%s *)%s{
if(!_%s){
_%s = [[%s alloc] initWithFrame:CGRectZero];
_%s.delegate = self;
_%s.backgroundColor = [UIColor whiteColor];
_%s.font = [UIFont systemFontOfSize:15];
_%s.textColor = [UIColor blackColor];
}
return _%s;
\n}''' % (className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue))
#生成 UITextView
def generateTextView(className='UITextView', classValue='defaultImageView'):
return ('''\n-(%s*)%s{
if(!_%s){
_%s = [[%s alloc] initWithFrame:CGRectZero];
_%s.delegate = self;
_%s.backgroundColor = [UIColor whiteColor];
_%s.font = [UIFont systemFontOfSize:15];
_%s.textColor = [UIColor blackColor];
}
return _%s;
\n}''' % (className,classValue,classValue,classValue,className,classValue,classValue,classValue,classValue,classValue))
# 默认的 生成对象
def generateDefaultClass(className='UILabel', classValue='defaultLabel'):
return ('''\n- (%s *)%s {\n\tif (!_%s) {\n\t\t_%s = [[%s alloc] init];\n\t}\n\treturn _%s;\n}\n''' % (className, classValue, classValue, classValue, className, classValue))
#config UI
def generateConfigUI(classValue='defaultLabel'):
return ('''\n\t[self.contentView addSubview:self.%s];\n\t''' % (classValue))
#config UI layout
def generateConfigUILayout(classValue='defaultLabel'):
return ('''\n\t[self.%s mas_makeConstraints:^(MASConstraintMaker *make) {\n\t\t \n\t}];\n\t''' % (classValue))
#config UI layout 1
def generateConfigUILayout1(classValue='defaultLabel'):
return ('''\n\t[self.%s mas_makeConstraints:^(MASConstraintMaker *make) {
//\tmake.centerX.equalTo(<#xx#>).offset(<#0#>);
//\tmake.centerY.equalTo(<#xx#>).offset(<#0#>);
//\tmake.height.mas_equalTo(<#0#>);
//\tmake.width.mas_equalTo(<#0#>);
//\tmake.left.equalTo(<#xx#>).offset(<#xx#>);
//\tmake.bottom.equalTo(<#xx#>).offset(<#xx#>);
//\tmake.top.equalTo(<#xx#>).offset(<#xx#>);
//\tmake.right.equalTo(<#xx#>).offset(<#xx#>);
\n\t}];\n''' % (classValue))
# file_path = input('输入目录地址:\n')
file_path = '/Users/mac/Desktop/Ejaculation/ELModules/ELMeModule/ELMeModule/Classes/View/ELMineEditPersonalVCFooterView.h'
autowrite(file_path.strip())
网友评论