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)
}
}
网友评论