一、有啥用:
CodeSnippet主要是為了減少開發(fā)人員敲重復(fù)代碼,試想一下一個頁面有10個UIButton,是不是每個UIButton都要敲一遍設(shè)置title、image、tittleColor、event這些屬性的代碼。
如果設(shè)置了常用的CodeSnippet,當(dāng)寫一個UIButton的時候,只需要輸入快捷鍵,就能把UIButton對應(yīng)的代碼片段自動打出來,接下來要做的只是把不需要的屬性刪掉,然后寫上自己對應(yīng)的屬性即可。
二、如何創(chuàng)建代碼片段
1.在文件空白處編寫代碼片段

image.png
2.選中該代碼片段,然后右擊選擇Create Code Snippet

image.png
3.分別輸入名稱和快捷鍵即可

image.png
或者直接將別人提供的代碼片段拷貝到你的代碼片段文件夾中
存放代碼片段的文件夾為
~/Library/Developer/Xcode/UserData/CodeSnippets
如果沒有CodeSnippets,自行創(chuàng)建
以下是我常用的代碼片段,自取
鏈接: https://pan.baidu.com/s/1mNYiPRiVhN9ILuatTEnvHQ 提取碼: 70u9
三、怎么用
以UIButton為例,我這邊UIButton相關(guān)的代碼片段快捷鍵:initbutton
當(dāng)輸入initbutton時,xcode會自動提示

輸入快捷鍵initbutton
按下回車鍵的時候就會把對應(yīng)的代碼片段寫入文件

image.png
接下來就是把對應(yīng)的屬性填上,把不需要的屬性刪了。
四、常用代碼片段
以下僅為個人常用的自定義代碼片段
屬性相關(guān)
- 值類型
//快捷鍵 @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#>;
初始化相關(guān)
- NSMutableArray
//快捷鍵 initmutablearray
<#propertyName#> = [[NSMutableArray alloc] init];
- NSMutableArray 懶加載
//快捷鍵 lazyinitmutablearray
- (NSMutableArray *)<#propertyName#>{
if (!_<#propertyName#>) {
_<#propertyName#> = [[NSMutableArray alloc] init];
}
return _<#propertyName#>;
}
- UIView 常規(guī)
//快捷鍵 initview
<#propertyName#> = [[UIView alloc] init];
<#propertyName#>.backgroundColor = <#UIColor#>;
- UIView - 懶加載
//快捷鍵 lazyinitview
- (UIView *)<#propertyName#>{
if (!_<#propertyName#>) {
_<#propertyName#> = [[UIView alloc] init];
_<#propertyName#>.backgroundColor = <#UIColor#>;
}
return _<#propertyName#>;
}
- UIImageView 常規(guī)
//快捷鍵 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 常規(guī)
//快捷鍵 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 常規(guī)
//快捷鍵 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 常規(guī)
<#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 常規(guī)
//快捷鍵 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 常規(guī)
//快捷鍵 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相關(guān)
//快捷鍵 layer
<#propertyName#>.layer.borderWidth = <#CGFloat#>;
<#propertyName#>.layer.borderColor = <#CGColor#>;
<#propertyName#>.layer.masksToBounds = <#BOOL#>;
<#propertyName#>.layer.cornerRadius = <#CGFloat#>;
- MJRefresh相關(guān)
//快捷鍵 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#>;
}