iOS学习零散笔记记录

在学习的过程中,特别是在查看其它人的代码时,会发现很多可以学习点,就在此进行记录。

1.添加头像轮廓/描边

类似下面,我们经常需要给头像添加一个圆边。

描边效果图

以前的写法:

     avatarView.layer.borderWidth = 1;
     avatarView.layer.borderColor = [UIColor grayColor];

新学到的写法(直接通过CALayer描边):

    //--添加头像外轮廓
    CALayer *avatarBorder = [CALayer layer];
    avatarBorder.frame = _avatarView.bounds;
    avatarBorder.borderWidth = CGFloatFromPixel(1);
    avatarBorder.borderColor = [UIColor colorWithWhite:0.000 alpha:0.090].CGColor;
    avatarBorder.cornerRadius = _avatarView.height / 2;
    avatarBorder.shouldRasterize = YES;
    avatarBorder.rasterizationScale = kScreenScale;
    [_avatarView.layer addSublayer:avatarBorder];

2. CGRectInset使用,获取坐标更简便

如下两种写法是一个效果,但是第二个更简便;

//1.
CGRectMake(avatarView.fram.origin.x, avatarView.fram.origin.y, 10, 10);
//2.
CGRectInset(avatarView.frame, 10, 10);

3. 调用[WBStatusView new] 会怎么样呢?

会去调用WBStatusView的initFrame方法;去看别人代码的时候不要傻乎乎的了!

4.保存坐标到字典/数组

如下,可以把坐标保存到字典/数组;

       NSMutableArray * tmpArray = [NSMutableArray arrayWithCapacity:0];
       [tmpArray addObject:[NSValue valueWithCGRect:CGRectMake(0, 0, 10, 10)]];
   

也可以使用[NSValue valueWithRange:<#(NSRange)#>][NSValue valueWithCGPoint:<#(CGPoint)#>]保存范围和点;
这样在对tableView的高度预计算的时候,就方便保存坐标到model;

5.获取绝对值

long labs(long) __pure2; 返回绝对值
eg:

      labs(-5);//得到5

6.获取tableView当前显示的cells和rows

用tableview的以下属性即可:
indexPathsForVisibleRows:所有可见的行;
visibleCells:所有可见的cell;

  if (tableView.indexPathsForVisibleRows.count<=0) {//--indexPathsForVisibleRows:所有可见的行
  }
  
  if (tableView.visibleCells&&self.visibleCells.count>0) {//visibleCells:所有可见的cell
  }

7.tableview上面添加图片需要注意

例如朋友圈需要显示9个图片,我们经常所有图片创建好,不需要的隐藏,需要的显示;
注意的是我们图片隐藏的时候,也要取消加载;不然还是占用资源;

    //--图片隐藏,并且取消加载
    for (UIImageView *thumb1 in mulitPhotoScrollView.subviews) {
        if (!thumb1.hidden) {
            [thumb1 sd_cancelCurrentImageLoad];
        }
    }

8.使用VVeboTableViewDemo计算文本大小、绘制文本

VVeboTableViewDemo封装了分类,可以直接计算文本大小、绘制文本。使用方法如下:

  CGSize size = [subData[@"text"] sizeWithConstrainedToWidth:width fromFont:FontWithSize(SIZE_FONT_SUBCONTENT) lineSpace:5];

其中,VVeboTableViewDemo 的UIView+Additions.m对文本计算的封装如下:

- (CGSize)sizeWithConstrainedToWidth:(float)width fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{
    return [self sizeWithConstrainedToSize:CGSizeMake(width, CGFLOAT_MAX) fromFont:font1 lineSpace:lineSpace];
}

- (CGSize)sizeWithConstrainedToSize:(CGSize)size fromFont:(UIFont *)font1 lineSpace:(float)lineSpace{
    CGFloat minimumLineHeight = font1.pointSize,maximumLineHeight = minimumLineHeight, linespace = lineSpace;
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)font1.fontName,font1.pointSize,NULL);
    CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
    //Apply paragraph settings
    CTTextAlignment alignment = kCTLeftTextAlignment;
    CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[6]){
        {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment},
        {kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight},
        {kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight},
        {kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(linespace), &linespace},
        {kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(linespace), &linespace},
        {kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode}
    },6);
    NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font,(NSString*)kCTFontAttributeName,(__bridge id)style,(NSString*)kCTParagraphStyleAttributeName,nil];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self attributes:attributes];
    //    [self clearEmoji:string start:0 font:font1];
    CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)string;
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
    CGSize result = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [string length]), NULL, size, NULL);
    CFRelease(framesetter);
    CFRelease(font);
    CFRelease(style);
    string = nil;
    attributes = nil;
    return result;
}

你可能感兴趣的:(iOS学习零散笔记记录)