swift-给UIButton添加下划线

2023-12-04  本文已影响0人  songjk
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)
        
        
    }
}
上一篇 下一篇

猜你喜欢

热点阅读