一、有啥用:
CodeSnippet主要是为了减少开发人员敲重复代码,试想一下一个页面有10个UIButton,是不是每个UIButton都要敲一遍设置title、image、tittleColor、event这些属性的代码。
如果设置了常用的CodeSnippet,当写一个UIButton的时候,只需要输入快捷键,就能把UIButton对应的代码片段自动打出来,接下来要做的只是把不需要的属性删掉,然后写上自己对应的属性即可。
二、如何创建代码片段
1.在文件空白处编写代码片段
2.选中该代码片段,然后右击选择Create Code Snippet
3.分别输入名称和快捷键即可
或者直接将别人提供的代码片段拷贝到你的代码片段文件夹中
存放代码片段的文件夹为
~/Library/Developer/Xcode/UserData/CodeSnippets
如果没有CodeSnippets,自行创建
以下是我常用的代码片段,自取
链接: https://pan.baidu.com/s/1mNYiPRiVhN9ILuatTEnvHQ 提取码: 70u9
三、怎么用
以UIButton为例,我这边UIButton相关的代码片段快捷键:initbutton
当输入initbutton时,xcode会自动提示
按下回车键的时候就会把对应的代码片段写入文件
接下来就是把对应的属性填上,把不需要的属性删了。
四、常用代码片段
以下仅为个人常用的自定义代码片段
属性相关
- 值类型
//快捷键 @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#>;
}