UIButton或UILabel加个下划线

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地址:

https://github.com/xuanyi0627/LXYHyperlinks.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS 9.0中,用户对通知可以快捷回复文本消息,需要设置动作的行为为输入文本,一般用户后台模式的按钮(前台就直...
    ShenYj阅读 1,104评论 2 5
  • 在幻像的世界里看魔——评《求魔2:邪蛮降临》 文/北风来袭 读玄幻(魔幻?仙侠?)书,你需要不由自主的展开你的想象...
    北风来袭阅读 1,367评论 1 2
  • “一个人可以爱一个人多久,一个人可以为一个人付出多少。两个人又能相守多久。”潮湿的夜晚,琼在她的日记本上写下这样...
    Sunday____阅读 189评论 0 0
  • 今日话题是我的婚恋观,回想起当时自己作为大龄女青年给自己订了个目标:30岁前结婚生子,巧的是30岁前一天儿子出世了...
    清透阅读 590评论 0 1
  • For those of you that I didn't have the pleasure of meeti...
    时见疏星阅读 456评论 0 0