美文网首页
swift-给UIButton添加下划线

swift-给UIButton添加下划线

作者: songjk | 来源:发表于2023-12-04 18:22 被阅读0次
extension UIButton{
    
    func setUnderLine() {
        let text = self.titleLabel?.text ?? ""
        let fontSize = self.titleLabel?.font.pointSize ?? 10
        let color = self.titleLabel?.textColor ?? .white
        let attStr = NSMutableAttributedString(string: text)
        let range = NSRange(location: 0, length: attStr.length)
        let number = NSNumber(integerLiteral: NSUnderlineStyle.single.rawValue)
        let attrs = [NSAttributedString.Key.underlineStyle:number,
                     NSAttributedString.Key.foregroundColor:color,
                     NSAttributedString.Key.font:self.titleLabel?.font ?? UIFont.systemFont(ofSize: fontSize)]
        attStr.addAttributes(attrs, range: range)
        self.setAttributedTitle(attStr, for: .normal)
        
        
    }
}

相关文章

网友评论

      本文标题:swift-给UIButton添加下划线

      本文链接:https://www.haomeiwen.com/subject/splrgdtx.html