美文网首页程序员
UITableviewCell下划线封装

UITableviewCell下划线封装

作者: Kevin_wzx | 来源:发表于2017-12-06 11:28 被阅读19次

对UITableviewCell的基础封装:下划线处理;所有其他的cell自定义、封装都可以继承这个基础类

.h

#import <UIKit/UIKit.h>

@interface UToTableViewCell : UITableViewCell

@property (nonatomic, assign) BOOL appearUnderline;         //  显示下滑线 默认为no
@property (nonatomic, assign) BOOL lineToleftSpace;         //  是否对左边有没有空间隔 yes 表示没有
@property (nonatomic, assign) BOOL lintTorightSpace;        //  石头对右边有没有空间隔 yes 表示有
@property (nonatomic, strong) UIColor *lineColor;           //  底边颜色

@end

.m

#import "UToTableViewCell.h"

@implementation UToTableViewCell

- (void)awakeFromNib {
    
    [super awakeFromNib];
}

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

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        [self initializeInfo];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    
    self = [super initWithCoder:coder];
    
    if (self) {
        [self initializeInfo];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initializeInfo];
    }
    return self;
}

- (void)initializeInfo {
    
    self.clipsToBounds = YES;
    [self setExclusiveTouch:YES];
    self.contentView.backgroundColor = [UIColor clearColor];
}

- (void)setAppearUnderline:(BOOL)appearUnderline {
    
    if (_appearUnderline == appearUnderline) {
        
        return;
    }
    _appearUnderline = appearUnderline;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    if (_appearUnderline) {
        
        CGContextRef ctf = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(ctf, _lineColor?_lineColor.CGColor:[UToColor separatelineGrayColor].CGColor);
        CGContextSetStrokeColorWithColor(ctf, _lineColor?_lineColor.CGColor:[UToColor separatelineGrayColor].CGColor);
        
        if (_lineToleftSpace) {
            
            CGContextMoveToPoint(ctf, 0, rect.size.height);
        } else {
            
            CGContextMoveToPoint(ctf, LeftMargin, rect.size.height);
        }
        
        if (!_lintTorightSpace) {
            
            CGContextAddLineToPoint(ctf, rect.size.width, rect.size.height);
        } else {
            
            CGContextAddLineToPoint(ctf, rect.size.width-RigthMargin, rect.size.height);
        }
        CGContextDrawPath(ctf, kCGPathFillStroke);
    }
}

@end

相关文章

网友评论

    本文标题:UITableviewCell下划线封装

    本文链接:https://www.haomeiwen.com/subject/ssecixtx.html