第一种:文字和下划线颜色相同

    private lazy var checkReasonBtn : UIButton = {
let btn = JYUIModel.createBtn()
btn.addTarget(self, action: #selector(clickCheckReasonBtn), for: UIControl.Event.touchUpInside)
var attributedString = NSMutableAttributedString(string:"查看原因")
var range = NSRange()
range.location =
range.length = attributedString.length
attributedString.addAttributes([NSAttributedString.Key.underlineStyle : NSNumber(value: NSUnderlineStyle.single.rawValue),NSAttributedString.Key.foregroundColor:UIColor.init(hexString: "#4A90E2")], range: range)
btn.setAttributedTitle(attributedString, for: UIControl.State.normal)
return btn
}()

第二种, 文字和下划线颜色不同

import UIKit

/// 带下划线的Button
class UnderlineButton: JYBaseButton { /// 下划线高度
var underLineHeight:CGFloat = 2{
didSet{
//保护
if (underLineHeight > self.bounds.size.height/2) || (underLineHeight < 0){
underLineHeight = 2
}
self.draw(self.bounds)
}
} /// 默认下划线颜色
var normalUnderColor:UIColor = UIColor.clear /// 选中下划线颜色
var selectedUnderColor:UIColor = UIColor.init(hexString: "#FF8437") convenience init(normalUnderColor:UIColor,
selectedUnderColor:UIColor,
underLineHeight:CGFloat){
self.init()
self.normalUnderColor = normalUnderColor
self.selectedUnderColor = selectedUnderColor
self.underLineHeight = underLineHeight
} override var isSelected: Bool{
didSet{
self.draw(self.bounds)
}
} override func draw(_ rect: CGRect) {
let textRect : CGRect = self.titleLabel?.frame ?? CGRect.zero
if let contect : CGContext = UIGraphicsGetCurrentContext(){ switch isSelected {
case true:
contect.setStrokeColor((selectedUnderColor.cgColor))
case false:
contect.setStrokeColor((normalUnderColor.cgColor))
}
contect.move(to: CGPoint(x: textRect.origin.x, y: self.bounds.size.height - underLineHeight) )
contect.addLine(to: CGPoint(x: textRect.origin.x + textRect.size.width, y: self.bounds.size.height - underLineHeight))
contect.setLineWidth(underLineHeight)
contect.closePath()
contect.drawPath(using: CGPathDrawingMode.stroke)
}
}
}

  

swift 带有下划线的UIbutton的更多相关文章

  1. 使用Java生成带有下划线字体的文字

    HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>(); hm.put(TextAtt ...

  2. Swift - 使用下划线(_)来分隔数值中的数字

    为了增强较大数值的可读性,Swift语言增加了下划线(_)来分隔数值中的数字. 不管是整数,还是浮点数,都可以使用下划线来分隔数字. 1 2 3 4 //数值可读性 let value1 = 10_0 ...

  3. PHP常见带有下划线的常量

    1.__PHP_Incomplete_Class <?php echo __PHP_Incomplete_Class::class; ?> __PHP_Incomplete_Class 2 ...

  4. mybatis无法给带有下划线属性赋值问题

    https://blog.csdn.net/qq_33768099/article/details/69569561

  5. 【界面优化】使用viewpagerindicator添加下划线滑动动画

    开源代码viewpagerindicator里面没有实现tab下划线切换过程中的移动动画,都是很突兀的多个fragement之间的切换,导致用户体验略差,google了下相关问题,发现一片博文: ht ...

  6. PowerPoint超链接字体颜色修改、怎么去掉超链接下划线

    经常在做PPT幻灯片时会遇到这样一个问题,给文字加超链接后发现链接的颜色是蓝色的,而且还带有下划线,这种效果与主题的色彩搭配简直是太影响美观效果了.有没有什么办法可以去掉PPT中的超链接下划线?再将超 ...

  7. TabTopUnderLineLayout【自定义顶部选项卡(带下划线)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线样式的效果. 备注:如果配合Fragment的话,MainActivit ...

  8. TabTopAutoLayout【自定义顶部选项卡区域(带下划线)(动态选项卡数据且可滑动)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线且可滑动效果.[实际情况中建议使用RecyclerView] 备注:如果 ...

  9. FragmentTabHostUnderLineDemo【FragmentTabHost带下划线】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(带下划线效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...

随机推荐

  1. 服务限流-令牌桶java实现

    此文非常不错,抄自: https://www.cnblogs.com/googlemeoften/p/6020718.html 其他实现 https://www.cnblogs.com/LBSer/p ...

  2. Linux下使用命令行配置IPMI

    ipmitool是什么: 百度百科给的解释已经够用了,简单说就是“IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备“智能 ...

  3. oc字符串与c字符串转换和拷贝

    // Helper method to create C string copy NSString* MakeNSString (const char* string) { if (string) { ...

  4. LeetCode OJ 143. Reorder List(两种方法,快慢指针,堆栈)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  5. Python读取图片尺寸、图片格式

    Python读取图片尺寸.图片格式 需要用到PIL模块,使用pip安装Pillow.Pillow是从PIL fork过来的Python 图片库. from PIL import Image im = ...

  6. JSP报错Syntax error, insert ";" to complete Statement

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 7 in ...

  7. 关于T-SQL中exists或者not exists子查询的“伪优化”的做法

    问题起源 在使用t-sql中的exists(或者not exists)子查询的时候,不知道什么时候开始,发现一小部分人存在一种“伪优化”的一些做法,并且向不明真相的群众传递这一种写法“优越性”,实在看 ...

  8. web前端面试题HTML/CSS部分

    web前端面试题HTML/CSS部分 前端页面有哪三层构成,分别是什么?作用是什么? 1.结构层:由 HTML 或 XHTML 之类的标记语言负责创建,仅负责语义的表达.解决了页面“内容是什么”的问题 ...

  9. C#图像处理:截图程序(包含鼠标)

    截图后在picbox中显示,用定时器定时每毫秒截图一次,在picbox上显示就有动画效果.代码: [DllImport("user32.dll")] static extern b ...

  10. C#中的 new Random()

    在C#中,产生随机数常用大方法是 new Random().Next(1,10)等方法. 但是仔细发现会有个问题: 看代码: ; i < ;i++ ) { Console.WriteLine(, ...