美文网首页
UnderLineButton

UnderLineButton

作者: 和女神经常玩 | 来源:发表于2022-12-30 18:23 被阅读0次

接口部分

@interface UnderLineButton : UIButton

@property (nonatomic,assign) CGSize lineSize;
@property (nonatomic,strong) UIColor *lineColor;
@property (nonatomic,assign) BOOL isFollowTextWidth;

-(void)setFont:(UIFont *)font isSelected:(BOOL)isSelected;


@end

实现部分

@interface UnderLineButton ()

@property (nonatomic,strong) UIView *underLine;
@property (nonatomic,strong) UIFont *normalFont;
@property (nonatomic,strong) UIFont *selectFont;

@end

@implementationSUnderLineButton

{
    CGSize _lineSize;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.underLine];
        self.isFollowTextWidth = YES;
        self.lineSize = CGSizeMake(frame.size.width, 3);
    }
    return self;
}

-(void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    if (selected) {
        self.underLine.hidden = NO;
        if (self.selectFont) {
            self.titleLabel.font = self.selectFont;
        }
    }
    else
    {
        self.underLine.hidden = YES;
        if (self.normalFont) {
            self.titleLabel.font = self.normalFont;
        }
    }
}
-(void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    [self layoutUnderLine];
}

-(UIView *)underLine
{
    if (_underLine == nil) {
        _underLine = [[UIView alloc] init];
        _underLine.backgroundColor = [UIColor whiteColor];
        _underLine.layer.cornerRadius = 1;
        _underLine.layer.masksToBounds = YES;
        _underLine.hidden = !self.selected;
        [self layoutUnderLine];
    }
    return _underLine;
}
-(void)setIsFollowTextWidth:(BOOL)isFollowTextWidth
{
    _isFollowTextWidth = isFollowTextWidth;
    [self layoutUnderLine];
}
-(void)setLineColor:(UIColor *)lineColor
{
    self.underLine.backgroundColor = lineColor;
}
-(UIColor *)lineColor
{
    return self.underLine.backgroundColor;
}
-(void)setLineSize:(CGSize)lineSize
{
    _lineSize = lineSize;
    [self layoutUnderLine];
}
-(CGSize)lineSize
{
    return self.underLine.frame.size;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutUnderLine];
}
-(void)layoutUnderLine
{
    if (self.isFollowTextWidth) {
        CGFloat lineW = [self.titleLabel.text getWidthWithFont:self.titleLabel.font height:self.frame.size.height];
        CGFloat lineH = _lineSize.height;
        self.underLine.frame = CGRectMake(self.frame.size.width / 2.0 - lineW / 2.0, self.frame.size.height - lineH, lineW, lineH);
    }
    else
    {
        CGFloat lineW = _lineSize.width;
        CGFloat lineH = _lineSize.height;
        self.underLine.frame = CGRectMake(self.frame.size.width / 2.0 - lineW / 2.0, self.frame.size.height - lineH, lineW, lineH);
    }
}


-(void)setFont:(UIFont *)font isSelected:(BOOL)isSelected;
{
    if (isSelected == YES) {
        self.selectFont = font;
    }
    else
    {
        self.normalFont = font;
    }
}

@end

相关文章

网友评论

      本文标题:UnderLineButton

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