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加个下划线相关推荐

  1. python3-xlwt-Excel设置表格基础(字体颜色 背景颜色 设置边框 边框颜色 冻结窗口 字体大小 字体高度 字体加粗 下划线 斜体字 单元格对齐方式 自动换行 删除线 超链接 插入公式)

    文章目录 引入xlwt和创建workboos对象 初始化样式和创建设置字体,赋值给style 保存文件 自定义样式 字体颜色 背景颜色 设置边框 边框颜色 冻结窗口 字体大小 字体高度 字体加粗 下划 ...

  2. Qt将选中的字体加粗下划线

    QTextCharFormat fmt; //需要头文件 #include <QTextEdit>fmt=ui->txtEdit->currentCharFormat(); / ...

  3. UIButton 上的标题添加下划线效果

    subclass UIButton类 重载drawrect方法: - (void)drawRect:(CGRect)rect {// Get the Render ContextCGContextRe ...

  4. ios 按钮下面加下划线_iOS开发UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...

  5. ios 按钮文字下划线_iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  6. 我可以在Android版式中加下划线吗?

    如何在Android布局xml文件中定义带下划线的文本? #1楼 Strings.xml文件内容: <resource><string name="my_text" ...

  7. TextView 加下划线 、 中划线

    有2中方法 //中画线 textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置 ...

  8. JavaScript中方法或者变量名称前加下划线的是什么意思?

    JavaScript中的方法或者变量名称前面有下划线,是做什么的? 加下划线不是js要求的,是程序员约定俗成的. 通常变量前加下划线表示"私有变量". 函数名前加下划线表示&quo ...

  9. 解决苹果iphone浏览器Safari把数字变色加下划线问题

    遇到的问题 今天在修改一个php项目时,有一个需求时是把选中的数字相加 在pc端浏览器试了没问题,在手机浏览器试了也可以,除了iphone手机(我没有苹果手机,穷得很,是用户反馈给我的)自带的Safa ...

最新文章

  1. Ubuntu安装Ceres库-安装依赖时报错:E:无法定位软件包 libcxsparse3.1.2_朱国鑫的博客-CSDN博客
  2. jquery等待特定元素加载再执行相关函数
  3. 64位linux下玩32位汇编编程
  4. java arrays.aslist()_Java中Arrays.asList()方法详解及实例
  5. 前端教程之Intro.js轻松实现新手引导效果
  6. 【渝粤题库】国家开放大学2021春1439临床药理学题目
  7. DVWA-SQL注入
  8. python stdin和stdout_stdin似乎比stdout(python)慢得多.为什么?
  9. js获取页面URL地址并判断URL是否包含具体值
  10. 在CentOS/RHEL 7.X安装 EPEL repo 的方法 1
  11. ado.net知识点博客网址
  12. 1*1的卷积核与Inception
  13. Android 汉字转拼音的多种实现方式
  14. ubuntu安装windows 字体
  15. 用FFmpeg保存JPEG图片
  16. 非宁静无以致远 非淡泊无以明志
  17. 做一个心无杂念的平凡人
  18. Python程序题练习
  19. 生死大PK:软路由是否会威胁到硬路由
  20. 王牌竞速安装后显示服务器维护,王牌竞速怎么修车 王牌竞速怎么维修车 王牌竞速的车怎么维护...

热门文章

  1. 远程mysql定时删除数据_mysql定时备份数据库 删除历史文件 将备份数据库传送到另外服务器...
  2. leetcode114. 二叉树展开为链表
  3. 如何使得客户端和服务器端完美配合做IOS应用内付费
  4. (一)容器从入门到深入-容器和镜像
  5. Pytorch(5)-梯度反向传播
  6. 《Python Cookbook 3rd》笔记(5.6):字符串的 I/O 操作
  7. 二十一世纪Windows简史
  8. 十二个生活习惯,增加你的心灵压力
  9. 大数据应用时代来袭 SaaS走向没落?
  10. ADOQuery 查询 删除 修改 插入