• swift 带有下划线的UIbutton


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

        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 = 0
            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)
            }
        }
    }
    

      

  • 相关阅读:
    Jenkins安装2
    11月14 vagrant 虚拟机连接
    11月13 ssh 和 netstat
    Jenkins安装
    11月12号 springboot1.5 引入redis
    11月12号 用户登录输入密码错误达到指定次数后,锁定账户 004
    js模式-观察者模式
    立即执行函数
    vue 获取数据
    在登陆退出时候使用Vuex
  • 原文地址:https://www.cnblogs.com/qingzZ/p/10405116.html
Copyright © 2020-2023  润新知