UIButton或UILabel加个下划线

本文介绍了一个自定义的UIButton实现,包括Objective-C和Swift两种语言版本。该按钮支持设置下划线颜色,通过调整颜色属性可以改变按钮文本下方的线条颜色。

UIButton

####Objective - C

LXYHyperlinksButton.h

@interface LXYHyperlinksButton : UIButton
{
    UIColor *lineColor;
}

-(void)setColor:(UIColor*)color;
复制代码

LXYHyperlinksButton.m

#import "LXYHyperlinksButton.h"

@implementation LXYHyperlinksButton

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

-(void)setColor:(UIColor *)color{
    lineColor = [color copy];
    [self setNeedsDisplay];
}


- (void) drawRect:(CGRect)rect {
    CGRect textRect = self.titleLabel.frame;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    CGFloat descender = self.titleLabel.font.descender;
    if([lineColor isKindOfClass:[UIColor class]]){
        CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor);
    }
    
    CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender+1);
    CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender+1);
    
    CGContextClosePath(contextRef);
    CGContextDrawPath(contextRef, kCGPathStroke);
}

@end

复制代码

####Swift

LXYHyperlinksButton.swift

import UIKit

class LXYHyperlinksButton: UIButton {
    
    var lineColor: UIColor!
    
    internal func setColor(color:UIColor) {
        if lineColor == nil {
            lineColor = UIColor.whiteColor()
        }
        lineColor = color.copy() as! UIColor
        self.setNeedsDisplay()
    }
    
    override func drawRect(rect: CGRect) {
        let textRect: CGRect = self.titleLabel!.frame
        let contextRef: CGContextRef = UIGraphicsGetCurrentContext()!
        let descender: CGFloat = self.titleLabel!.font.descender
        if lineColor.isKindOfClass(UIColor) {
            CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor)
        }
        CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender + 1)
        CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender + 1)
        CGContextClosePath(contextRef)
        CGContextDrawPath(contextRef, .Stroke)
    }

}
复制代码

##UILabel

LXYHyperlinksLabel.h

@interface LXYHyperlinksLabel : UILabel
{
    UIColor *lineColor;
}

-(void)setColor:(UIColor*)color;
复制代码

LXYHyperlinksLabel.m

#import "LXYHyperlinksLabel.h"

@implementation LXYHyperlinksLabel

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

-(void)setColor:(UIColor *)color{
    lineColor = [color copy];
    [self setNeedsDisplay];
}


- (void) drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGRect textRect = self.frame;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    CGFloat descender = self.font.descender;
    if([lineColor isKindOfClass:[UIColor class]]){
        CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor);
    }
    
    CGContextMoveToPoint(contextRef, descender+1, textRect.size.height + descender+1);
    CGContextAddLineToPoint(contextRef, textRect.size.width, textRect.size.height + descender+1);
    CGContextClosePath(contextRef);
    CGContextDrawPath(contextRef, kCGPathStroke);
}

@end

复制代码

####Git地址:

github.com/xuanyi0627/…

转载于:https://juejin.im/post/5a3781aa51882512d0607429

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值