Xcode 自定义代码片段

1.定义

代码片段(Code Snippets),能够快速的完成经常使用的代码,提升效率。例如输入if回车出现的就是系统已经定义好的代码片段。

2.复用

自定义的代码片段是能直接拷贝的,即使换电脑后也能直接复用,储存位置为:
/用户/~/Library/Developer/Xcode/UserData/CodeSnippets

3.创建

选中代码块,按住option,拖动代码块到右侧位置,弹出编辑框

编辑.jpeg

使用.jpeg

4.常用代码块整理

4.0 其他常用功能

Title: weakSelf
Completion Shortcut: weakSelf
__weak typeof(self) weakSelf = self;

Title: 一句移除所有子控件
Completion Shortcut: ** removeFormSuper**
[<#view#>.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Title: SharedInstance
Completion Shortcut: SharedInstance

+ (instancetype)sharedInstance
{
    static id _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

Title: 设置阴影
Completion Shortcut: setShadow

<#view#>.layer.shadowColor = [UIColor whiteColor].CGColor;
<#view#>.layer.shadowOpacity = 1.0f;
<#view#>.layer.shadowRadius = 5.f;
<#view#>.layer.shadowOffset = CGSizeMake(0,0);
4.1变量声明

Title: Static NSString
Completion Shortcut: @staticString
static NSString* const <#name#> = <#value#>;

Title: Static NSInteger
Completion Shortcut: @staticInt
static NSInteger const <#name#> = <#value#>;

4.2 声明

Title: @property - assign
Completion Shortcut: @assign
property (nonatomic, assign) <#type#> *<#name#>;

Title: @property - strong
Completion Shortcut: @strong
property (nonatomic, strong) <#type#> *<#name#>;

Title: @property - copy
Completion Shortcut: @copy
property (nonatomic, copy) <#type#> *<#name#>;

Title: @property - weak
Completion Shortcut: @weak
property (nonatomic, weak) <#type#> *<#name#>;

Title: @property - delegate
Completion Shortcut: @delegate
property (nonatomic,weak) id<<#protocol#>> <#delegate#>;

4.3 注释

Title: 注释-1行
Completion Shortcut: Mark1
#pragma mark - <#name#>

Title: 注释-2行
Completion Shortcut: Mark2
#pragma mark
#pragma mark --

4.4.1 UI --- Label

Title: AttributedLabel
Completion Shortcut: attributedLabel

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];
4.4.2 UI --- TableView

Title: TableView-初始化
Completion Shortcut: getTableView

- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        
        _tableView.estimatedRowHeight = 80;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setSeparatorColor:[UIColor whiteColor]];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    return _tableView;
}

Title: TableView-数据源方法
Completion Shortcut: ** getTableViewMethod**

#pragma mark
#pragma mark -- TableView DataSource/Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return <#count#>;
}
- (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
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return <#rowHeight#>;
}

Title: TableViewCell-重写
Completion Shortcut: getTableViewCell

+ (RTHomeCell *)getHomeCellWithTableView:(UITableView *)tableView ID:(NSString *)ID{
    
    RTHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
        cell.backgroundColor = [UIColor clearColor];
        FTView *selView = [FTView getViewWithFrame:cell.frame bgColor:UIColorFromRGB(0x5be4fd,0.2)];
        cell.selectedBackgroundView = selView;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
}
4.4.3 UI - CollectionView

Title: CollectionView-初始化
Completion Shortcut: getCollectionView

- (void)collectionView {
    if(!_collectionView){
        // 1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(<#itemWidth#>, <#itemHeight#>);
        layout.minimumInteritemSpacing = <#间距#>;
        layout.minimumLineSpacing = <#行距#>;
        
        // 2.初始化collectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
        [<#view#> addSubview:_collectionView];
        
        // 3.注册cell
        [_collectionView registerClass:[<#CellClass#> class] forCellWithReuseIdentifier:<#CellID#>];
    }
    return _collectionView;
}

Title: CollectionView-数据源方法
Completion Shortcut: getCollectionViewMethod

#pragma mark
#pragma mark -- CollectionView DataSource/Delegate

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell* cell =  [collectionView dequeueReusableCellWithReuseIdentifier:<#CellID#> forIndexPath:indexPath];
   
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

Title: CollectionView-停在固定位置
Completion Shortcut: getCollectionViewStopFixedposition


// Cell滚动到指定位置
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    // topCollView 滑动时停在指定位置
    if (scrollView == _topCollView) {
        CGPoint originalTargetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
        CGPoint targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(self.topCollView.bounds)/2, CGRectGetHeight(self.topCollView.bounds) / 2);
        NSIndexPath *indexPath = nil;
        NSInteger i = 0;
        while (indexPath == nil) {
            targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(self.topCollView.bounds)/2 + 10*i, CGRectGetHeight(self.topCollView.bounds) / 2);
            indexPath = [self.topCollView indexPathForItemAtPoint:targetCenter];
            detailIndex = indexPath.item;
            i++;
        }
        //    self.selectedIndex = indexPath;
        //这里用attributes比用cell要好很多,因为cell可能因为不在屏幕范围内导致cellForItemAtIndexPath返回nil
        UICollectionViewLayoutAttributes *attributes = [self.topCollView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
        if (attributes) {
            *targetContentOffset = CGPointMake(attributes.center.x - CGRectGetWidth(self.topCollView.bounds)/2+30, originalTargetContentOffset.y);
        } else {
            //            NSLog(@"center is %@; indexPath is {%@, %@}; cell is %@",NSStringFromCGPoint(targetCenter), @(indexPath.section), @(indexPath.item), attributes);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,637评论 1 318
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,443评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,164评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,075评论 0 188
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,080评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,365评论 1 184
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,901评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,649评论 0 176
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,122评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,734评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,093评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,548评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,028评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,765评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,291评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,162评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,293评论 2 242

推荐阅读更多精彩内容