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,096評論 2 5
  • 在幻像的世界里看魔——評《求魔2:邪蠻降臨》 文/北風來襲 讀玄幻(魔幻?仙俠?)書,你需要不由自主的展開你的想象...
    北風來襲閱讀 1,318評論 1 2
  • “一個人可以愛一個人多久,一個人可以為一個人付出多少。兩個人又能相守多久。”潮濕的夜晚,瓊在她的日記本上寫下這樣...
    Sunday____閱讀 188評論 0 0
  • 今日話題是我的婚戀觀,回想起當時自己作為大齡女青年給自己訂了個目標:30歲前結婚生子,巧的是30歲前一天兒子出世了...
    清透閱讀 576評論 0 1
  • For those of you that I didn't have the pleasure of meeti...
    時見疏星閱讀 389評論 0 0