Quantcast
Channel: Rainbird的个人博客 »翻译
Viewing all articles
Browse latest Browse all 6

xcode4自定义文件模板(Creating Custom Xcode 4 File Templates)

0
0

翻译:http://www.bobmccune.com/2012/03/04/creating-custom-xcode-4-file-templates/

其实说是翻译有些牵强。我想实现的东西很简单:每次新创建类文件的时候都会自带一些函数,如:

@interface ViewController : UIViewController
@end

我希望是:

@interface ViewController:UIViewController{
}

@end

还有:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我希望是

#import "ViewController.h"

@interface ViewController()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
  return self;
}

- (void)viewDidLoad{
  [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

简单说来就是:
我希望.h文件里:两边儿的东西是挨着的;然后帮我加个{},应该是分两行的,{在文件尾。
我希望.m文件里:缩近的tab是两个空格;然后帮我加个{},应该是分两行的,{在文件尾。
就这个小需求就找了挺久,找不到,不过今天还是无意间让我给翻到了上面的英文。那篇文章很中规中距,是讲怎样自定义属于自己的模板,类似你要用js或者php开发,要新加一个板块儿那种。而我的需求要简单的多,就是改一下默认的配置。ok,开工:
首先说明模板文件位置
mac的模板文件在:

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates

ios的模拟文件在:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates

(其实这个要看实际情况,因为如果默认安装的话会是安装在/Applications下,但是像我这种不守规矩的,就把xcode.app搁到宿主目录下的application文件夹里了)
找到模板文件以后就是修改了。
原谅不建议大家这个目录修改现在的文件,强烈要求大家把这个路径下的文件拷贝到

~/Library/Developer/Xcode/Templates/File Templates/

如果路径不存在就创建。
我开始没认真看,然后照做了,然后就发现我新建议文件的时候,模板都是两份儿,名字都一下。后来想想也对,人家是为了自定义自己用的模板,而我是为了修改系统模板怎么吧?我觉得不改路径,在原始目录里操作,当然了,作为了一个好习惯把File Templates复制一份儿备份。
ok,接下来就是体力活了,你要一个一个的文件夹进去,然后分别修改.h和.m文件,然后一个一个接着找下去,而我呢,属于“工欲善其事,必先利其器”的那种,这么多文件哪受的了啊?想想这么多文件用脚本改也不合适,万一遇到不一样的就死了,还是手工操作,但是是怎么操作呢?
Finder->Go to the folder输入:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates


然后在搜索框里输入:filebasename

是不是都乖乖的集合了?
然后就是分批来,先整.h的
把所有的.h文件全选,右键,然后用xcode打开

然后就是一个一个改喽

.h改起来还算好改,要改的东西少,一遍是删除:两边儿空格,一遍是粘帖{}。要说的一点就是,我xcode设置成了自动保存,所以可以少按一遍command+s,改完以后直接关闭就全保存了。
完后就是如法儿泡制.m文件了。
改{}好改,直接command+e到文件尾,然后command+k下一行的{自动合到这一行了。tab改成两个的话更简单,因为我xcode里的设置就是tab是两个空格,这时候只要在文件里command+a全选,然后command+剪切,然后command+粘贴就要以了。xcode会自动帮你2个空格缩近。
就这样就好了,xcode甚至不用重启就可以直接用新创建的模板了。

如果你和我一样,喜欢两个空格缩近,{这个东东在行尾的话,你可直接下载下面的附件了。不然,就照着上边儿方法儿改一遍呗
File Templates


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images