美文网首页
Xcode Code Snippets自定义代码块

Xcode Code Snippets自定义代码块

作者: 进击的小恶魔 | 来源:发表于2017-11-01 13:56 被阅读320次

    iOS开发中,经常需要用一些定式的代码,比如一些控件的创建与设置,使用Code Snippets(代码块)可以大大提高开发的效率,也可以避免写代码时忘记做一些常规的处理,非常方便。

    自定义Code Snippets:
    在Xcode 9.0之前,可以拖代码到右侧的{ }中,然后处理,Xcode9.0之后无法选择并拖动代码块了,只能将以前写好的代码块复制到系统文件夹,然后重启Xcode即可。
    自定义代码块的目录:
    ~/Library/Developer/Xcode/UserData/CodeSnippets
    (可以使用github管理)

    系统自带的Code Snippets目录:
    /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
    可以用sublime等编辑工具打开文件,然后做修改,建议先备份。

    自定义Code Snippets:

    Title: strong
    Completion Shortcut: @strong
    Completion Scopes: Class Interface Methods
    
    @property (nonatomic, strong) <#type#> <#name#>;
    
    Title: buttoninit
    Completion Shortcut: @buttoninit 
    Completion Scopes: Function or Method
    
    UIButton *button = [[UIButton alloc] init];
    button.backgroundColor = [UIColor <#backgroundColor#>];
    button.titleLabel.font = [UIFont <#font#>];
    [button setTitle:<#title#> forState:UIControlStateNormal];
    [button setTitleColor:[UIColor <#titleColor#>] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [<#view#> addSubview:button];
    
    Title: borderButton
    Completion Shortcut: @borderbutton 
    Completion Scopes: Function or Method
    
    UIButton *borderButton = [[UIButton alloc] init];
    borderButton.layer.borderColor = [UIColor <#color#>].CGColor;
    borderButton.layer.borderWidth = <#borderWidth#>;
    borderButton.titleLabel.font = [UIFont <#font#>];
    borderButton.clipsToBounds = YES;
    borderButton.layer.cornerRadius = <#cornerRadius#>;
    borderButton.backgroundColor = [UIColor <#backgroundColor#>];
    [borderButton setTitleColor:[UIColor <#titleColor#>] forState:UIControlStateNormal];
    [borderButton setTitle:<#title#> forState:UIControlStateNormal];
     [borderButton addTarget:self action:@selector(borderButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [<#code#> addSubview:borderButton];
    
    Title: labelinit
    Completion Shortcut: @labelinit 
    Completion Scopes: Function or Method
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont <#font#>];
    label.text = <#text#>
    label.textColor = [UIColor <#textColor#>];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [<#view#> addSubview:label];
    
    Title: attributedLabel
    Completion Shortcut: @attributedLabel 
    Completion Scopes: Function or Method
    
    UILabel *attributedLabel =[[UILabel alloc] init];
    attributedLabel.numberOfLines = 0;
    attributedLabel.preferredMaxLayoutWidth = <#preferredMaxLayoutWidth#>;
    attributedLabel.backgroundColor = [UIColor clearColor];
    NSString *text = <#text#>;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = <#lineSpacing#>;
    NSDictionary *attr = @{
                               NSFontAttributeName: [UIFont <#font#>],
                               NSParagraphStyleAttributeName: style,
                               NSForegroundColorAttributeName: [UIColor <#color#>]
                               };
    attributedLabel.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attr];
    [<#view#> addSubview:attributedLabel];
    
    Title: tableinit
    Completion Shortcut: @tableinit 
    Completion Scopes: Function or Method
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [tableView registerClass:[<#classCell#> class] forCellReuseIdentifier:<#kReuseIdentifier#>];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor=[UIColor colorWithHex:<#0xFFFFFF#>];
    [<#view#> addSubview:tableView];
    
    Title: tableDelegate
    Completion Shortcut: @tableDelegate 
    Completion Scopes: Class Implementation
    
    #pragma mark - UITableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return <#count#>;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        <#classCell#> *cell = [tableView dequeueReusableCellWithIdentifier:<#kReuseIdentifier#> forIndexPath:indexPath];
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    }
    
    #pragma mark - UITableViewDelegate
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return <#rowHeight#>;
    }
    

    此外,可以根据自己的项目,做一些自定义的代码块,开发效率定会大大提高。

    相关文章

      网友评论

          本文标题:Xcode Code Snippets自定义代码块

          本文链接:https://www.haomeiwen.com/subject/nbiwpxtx.html