iOS CodeSnippet加速你的开发

一、有啥用:

CodeSnippet主要是为了减少开发人员敲重复代码,试想一下一个页面有10个UIButton,是不是每个UIButton都要敲一遍设置title、image、tittleColor、event这些属性的代码。

如果设置了常用的CodeSnippet,当写一个UIButton的时候,只需要输入快捷键,就能把UIButton对应的代码片段自动打出来,接下来要做的只是把不需要的属性删掉,然后写上自己对应的属性即可。

二、如何创建代码片段

1.在文件空白处编写代码片段


image.png

2.选中该代码片段,然后右击选择Create Code Snippet


image.png

3.分别输入名称和快捷键即可


image.png

或者直接将别人提供的代码片段拷贝到你的代码片段文件夹中
存放代码片段的文件夹为

~/Library/Developer/Xcode/UserData/CodeSnippets

如果没有CodeSnippets,自行创建

以下是我常用的代码片段,自取
链接: https://pan.baidu.com/s/1mNYiPRiVhN9ILuatTEnvHQ 提取码: 70u9

三、怎么用

以UIButton为例,我这边UIButton相关的代码片段快捷键:initbutton
当输入initbutton时,xcode会自动提示


输入快捷键initbutton

按下回车键的时候就会把对应的代码片段写入文件

image.png

接下来就是把对应的属性填上,把不需要的属性删了。

四、常用代码片段

以下仅为个人常用的自定义代码片段

属性相关

  • 值类型
//快捷键 @string
@property (strong, nonatomic) NSString *<#propertyName#>;
//快捷键 @number
@property (strong, nonatomic) NSNumber *<#propertyName#>;
//快捷键 @bool
@property (assign, nonatomic) BOOL <#propertyName#>;
//快捷键 @integer
@property (assign, nonatomic) NSInteger <#propertyName#>;
//快捷键 @float
@property (assign, nonatomic) CGFloat <#propertyName#>;
  • UI类型
//快捷键 @view
@property (strong, nonatomic) UIView *<#propertyName#>;
//快捷键 @imageview
@property (strong, nonatomic) UIImageView *<#propertyName#>;
//快捷键 @label
@property (assign, nonatomic) UILabel <#propertyName#>;
//快捷键 @button
@property (assign, nonatomic) UIButton <#propertyName#>;
//快捷键 @textfield
@property (assign, nonatomic) UITextFiled <#propertyName#>;
//快捷键 @textview
@property (assign, nonatomic) UITextView <#propertyName#>;
//快捷键 @tableview
@property (assign, nonatomic) UITableView <#propertyName#>;
//快捷键 @collectionview
@property (assign, nonatomic) UICollectionView <#propertyName#>;

初始化相关

  • NSMutableArray
//快捷键 initmutablearray
<#propertyName#> = [[NSMutableArray alloc] init];
  • NSMutableArray 懒加载
//快捷键 lazyinitmutablearray
- (NSMutableArray *)<#propertyName#>{
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[NSMutableArray alloc] init];
    }
    return _<#propertyName#>;
}
  • UIView 常规
//快捷键 initview
<#propertyName#> = [[UIView alloc] init];
<#propertyName#>.backgroundColor = <#UIColor#>;
  • UIView - 懒加载
//快捷键 lazyinitview
- (UIView *)<#propertyName#>{
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UIView alloc] init];
        _<#propertyName#>.backgroundColor = <#UIColor#>;
    }
    return _<#propertyName#>;
}
  • UIImageView 常规
    //快捷键 initimageview
    <#propertyName#> = [[UIImageView alloc] init];
    <#propertyName#>.image = <#UIImage#>;
    <#propertyName#>.contentMode = <#UIViewContentMode#>;
  • UIImageView 懒加载
//快捷键 lazyinitimageview
- (UIImageView *)<#propertyName#>{
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UIImageView alloc] init];
        _<#propertyName#>.image = <#UIImage#>;
        _<#propertyName#>.contentMode = <#UIViewContentMode#>;
    }
    return _<#propertyName#>;
}
  • UILabel 常规
    //快捷键 initlabel
    <#propertyName#> = [[UILabel alloc] init];
    <#propertyName#>.backgroundColor = <#UIColor#>;
    <#propertyName#>.text = <#NSString#>;
    <#propertyName#>.textColor = <#UIColor#>;
    <#propertyName#>.numberOfLines = <#NSInteger#>;
    <#propertyName#>.font = <#UIFont#>;
  • UILabel 懒加载
//快捷键 lazyinitlabel
- (UILabel *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UILabel alloc] init];
        _<#propertyName#>.backgroundColor = <#UIColor#>;
        _<#propertyName#>.text = <#NSString#>;
        _<#propertyName#>.textColor = <#UIColor#>;
        _<#propertyName#>.numberOfLines = <#NSInteger#>;
        _<#propertyName#>.font = <#UIFont#>;
    }
    return _<#propertyName#>;
}
  • UIButton 常规
    //快捷键 initbutton
    <#propertyName#> = [[UIButton alloc] init];
    
    <#propertyName#>.backgroundColor = <#UIColor#>;
    <#propertyName#>.titleLabel.font = <#UIFont#>;
    <#propertyName#>.titleLabel.numberOfLines = <#NSInteger#>;
    
    [<#propertyName#> setTitle:<#NSString#> forState:UIControlStateNormal];
    [<#propertyName#> setTitle:<#NSString#> forState:UIControlStateHighlighted];
    [<#propertyName#> setTitle:<#NSString#> forState:UIControlStateSelected];
    [<#propertyName#> setTitle:<#NSString#> forState:UIControlStateDisabled];
    
    [<#propertyName#> setTitleColor:<#UIColor#> forState:UIControlStateNormal];
    [<#propertyName#> setTitleColor:<#UIColor#>forState:UIControlStateHighlighted];
    [<#propertyName#> setTitleColor:<#UIColor#>forState:UIControlStateSelected];
    [<#propertyName#> setTitleColor:<#UIColor#> forState:UIControlStateDisabled];
    
    [<#propertyName#> setImage:<#UIImage#> forState:UIControlStateNormal];
    [<#propertyName#> setImage:<#UIImage#>  forState:UIControlStateHighlighted];
    [<#propertyName#> setImage:<#UIImage#> forState:UIControlStateSelected];
    [<#propertyName#> setImage:<#UIImage#>  forState:UIControlStateDisabled];
    
    [<#propertyName#> addTarget:self action:@selector(<#SEL#>) forControlEvents:UIControlEventTouchUpInside];
  • UIButton 懒加载
//快捷键 lazyinitbutton
- (UIButton *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UIButton alloc] init];
        
        _<#propertyName#>.backgroundColor = <#UIColor#>;
        _<#propertyName#>.titleLabel.font = <#UIFont#>;
        _<#propertyName#>.titleLabel.numberOfLines = <#NSInteger#>;
        
        [_<#propertyName#> setTitle:<#NSString#> forState:UIControlStateNormal];
        [_<#propertyName#> setTitle:<#NSString#> forState:UIControlStateHighlighted];
        [_<#propertyName#> setTitle:<#NSString#> forState:UIControlStateSelected];
        [_<#propertyName#> setTitle:<#NSString#> forState:UIControlStateDisabled];
        
        [_<#propertyName#> setTitleColor:<#UIColor#> forState:UIControlStateNormal];
        [_<#propertyName#> setTitleColor:<#UIColor#>forState:UIControlStateHighlighted];
        [_<#propertyName#> setTitleColor:<#UIColor#>forState:UIControlStateSelected];
        [_<#propertyName#> setTitleColor:<#UIColor#> forState:UIControlStateDisabled];
        
        [_<#propertyName#> setImage:<#UIImage#> forState:UIControlStateNormal];
        [_<#propertyName#> setImage:<#UIImage#>  forState:UIControlStateHighlighted];
        [_<#propertyName#> setImage:<#UIImage#> forState:UIControlStateSelected];
        [_<#propertyName#> setImage:<#UIImage#>  forState:UIControlStateDisabled];
        
        [_<#propertyName#> addTarget:self action:@selector(<#SEL#>) forControlEvents:UIControlEventTouchUpInside];
    }
    return _<#propertyName#>;
}

//快捷键 inittextfield

  • UITextField 常规
    <#propertyName#> = [[UITextField alloc] init];
    <#propertyName#>.text = <#NSString#>;
    <#propertyName#>.placeholder = <#NSString#>;
    <#propertyName#>.font = <#UIFont#>;
    <#propertyName#>.textColor = <#UIColor#>;
    <#propertyName#>.textAlignment = <#NSTextAlignment#>;
    <#propertyName#>.borderStyle = <#UITextBorderStyle#>;
  • UITextField 懒加载
//快捷键 lazyinittextfield
- (UITextField *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UITextField alloc] init];
        _<#propertyName#>.text = <#NSString#>;
        _<#propertyName#>.placeholder = <#NSString#>;
        _<#propertyName#>.font = <#UIFont#>;
        _<#propertyName#>.textColor = <#UIColor#>;
        _<#propertyName#>.textAlignment = <#NSTextAlignment#>;
        _<#propertyName#>.borderStyle = <#UITextBorderStyle#>;
    }
    return _<#propertyName#>;
}
  • UITextView 常规
    //快捷键 inittextview
    <#propertyName#> = [[UITextView alloc] init];
    <#propertyName#>.text = <#NSString#>;
    <#propertyName#>.placeholder = <#NSString#>;
    <#propertyName#>.font = <#UIFont#>;
    <#propertyName#>.textColor = <#UIColor#>;
    <#propertyName#>.textAlignment = <#NSTextAlignment#>;

    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = <#NSString#>;
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = <#UIColor#>;
    placeHolderLabel.font = <#UIFont#>;
    [placeHolderLabel sizeToFit];
    [<#propertyName#> addSubview:placeHolderLabel];

    [<#propertyName#> setValue:placeHolderLabel forKey:@"_placeholderLabel"];
  • UITextView 懒加载
//快捷键 lazyinittextview
- (UITextView *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UITextView alloc] init];
        _<#propertyName#>.text = <#NSString#>;
        _<#propertyName#>.placeholder = <#NSString#>;
        _<#propertyName#>.font = <#UIFont#>;
        _<#propertyName#>.textColor = <#UIColor#>;
        _<#propertyName#>.textAlignment = <#NSTextAlignment#>;
        
        UILabel *placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.text = <#NSString#>;
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = <#UIColor#>;
        placeHolderLabel.font = <#UIFont#>;
        [placeHolderLabel sizeToFit];
        [_<#propertyName#> addSubview:placeHolderLabel];

        [_<#propertyName#> setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    }
    return _<#propertyName#>;
}
  • UITableView 常规
    //快捷键 inittableview
    <#propertyName#> = [[UITableView alloc] initWithFrame:CGRectZero style:<#UITableViewStyle#>];
    <#propertyName#>.separatorColor = <#UIColor#>;
    <#propertyName#>.backgroundColor = <#UIColor#>;
    <#propertyName#>.showsVerticalScrollIndicator = NO;
    <#propertyName#>.showsHorizontalScrollIndicator = NO;
    <#propertyName#>.estimatedRowHeight = 0;
    <#propertyName#>.estimatedSectionFooterHeight = 0;
    <#propertyName#>.estimatedSectionHeaderHeight = 0;
    <#propertyName#>.tableFooterView = [UIView new];
    <#propertyName#>.delegate = self;
    <#propertyName#>.dataSource = self;
    
    [<#propertyName#> registerClass:[<#CellClass#> class] forCellReuseIdentifier:[<#CellClass#> reuseIdentifier]];
  • UITableView 懒加载
//快捷键 lazyinittableview
- (UITableView *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UITableView alloc] initWithFrame:CGRectZero style:<#UITableViewStyle#>];
        _<#propertyName#>.separatorColor = <#UIColor#>;
        _<#propertyName#>.backgroundColor = <#UIColor#>;
        _<#propertyName#>.showsVerticalScrollIndicator = NO;
        _<#propertyName#>.showsHorizontalScrollIndicator = NO;
        _<#propertyName#>.estimatedRowHeight = 0;
        _<#propertyName#>.estimatedSectionFooterHeight = 0;
        _<#propertyName#>.estimatedSectionHeaderHeight = 0;
        _<#propertyName#>.tableFooterView = [UIView new];
        _<#propertyName#>.delegate = self;
        _<#propertyName#>.dataSource = self;
        
        [_<#propertyName#> registerClass:[<#CellClass#> class] forCellReuseIdentifier:[<#CellClass#> reuseIdentifier]];
    }
    return _<#propertyName#>;
}
  • UICollectionView 初始化
    //快捷键 initcollectionview
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    <#propertyName#> = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    <#propertyName#>.collectionViewLayout = layout;
    <#propertyName#>.delegate = self;
    <#propertyName#>.dataSource = self;
    <#propertyName#>.showsHorizontalScrollIndicator = NO;
    <#propertyName#>.showsVerticalScrollIndicator = NO;
    <#propertyName#>.backgroundColor = <#UIColor#>;
    [<#propertyName#> registerClass:[<#CellClass#> class] forCellWithReuseIdentifier:[<#CellClass#> reuseIdentifier]];
  • UICollectionView 懒加载
//快捷键 lazyinitcollectionview
- (UICollectionView *)<#propertyName#> {
    if (!_<#propertyName#>) {
        _<#propertyName#> = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _<#propertyName#>.collectionViewLayout = layout;
        _<#propertyName#>.delegate = self;
        _<#propertyName#>.dataSource = self;
        _<#propertyName#>.showsHorizontalScrollIndicator = NO;
        _<#propertyName#>.showsVerticalScrollIndicator = NO;
        _<#propertyName#>.backgroundColor = <#UIColor#>;
        [_<#propertyName#> registerClass:[<#CellClass#> class] forCellWithReuseIdentifier:[<#CellClass#> reuseIdentifier]];
    }
    return _<#propertyName#>;
}
  • Layer相关
    //快捷键 layer
    <#propertyName#>.layer.borderWidth = <#CGFloat#>;
    <#propertyName#>.layer.borderColor = <#CGColor#>;
    <#propertyName#>.layer.masksToBounds = <#BOOL#>;
    <#propertyName#>.layer.cornerRadius = <#CGFloat#>;
  • MJRefresh相关
    //快捷键 listheaderfooter
    WEAK_SELF
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        STRONG_SELF
        [self refreshData];
    }];
    
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        STRONG_SELF
        [self loadMore];
    }];
    
    <#propertyName#>.mj_header = header;
    <#propertyName#>.mj_footer = footer;
  • Delegate - UITableView
//快捷键 delegatetable
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return <#NSInteger#>;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return <#NSInteger#>;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   NSInteger section = indexPath.section;
   
   <#CellClass#> *cell = [tableView dequeueReusableCellWithIdentifier:[<#CellClass#> reuseIdentifier] forIndexPath:indexPath];

   return cell;
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
   return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   return [[UIView alloc] initWithFrame:CGRectZero];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   return [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
  • Delegate - UICollectionView
//快捷键 delegatecollection
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return <#NSInteger#>;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return <#NSInteger#>;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    <#CellClass#> *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[<#CellClass#> reuseIdentifier] forIndexPath:indexPath];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(<#CGFloat#>, <#CGFloat#>);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(<#CGFloat#>, <#CGFloat#> ,<#CGFloat#> ,<#CGFloat#>);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return <#CGFloat#>;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return <#CGFloat#>;
}
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容