UIButton或UILabel加个下划线
生活随笔
收集整理的這篇文章主要介紹了
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 UIKitclass LXYHyperlinksButton: UIButton {var lineColor: UIColor!internal func setColor(color:UIColor) {if lineColor == nil {lineColor = UIColor.whiteColor()}lineColor = color.copy() as! UIColorself.setNeedsDisplay()}override func drawRect(rect: CGRect) {let textRect: CGRect = self.titleLabel!.framelet contextRef: CGContextRef = UIGraphicsGetCurrentContext()!let descender: CGFloat = self.titleLabel!.font.descenderif 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
總結
以上是生活随笔為你收集整理的UIButton或UILabel加个下划线的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无穷大功率电源matlab仿真,MATL
- 下一篇: matlab中的tfrwv函数,求助,关