如何使下划线使用Swift?



示例文本图像

我想画像下划线的图片。但"NSUnderlineStyleAttributeName"不起作用请帮帮我

你可以使用NSAttributedString

的例子:

let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue]
let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute)
YourLabel.attributedText = underlineAttributedString
<<p>扩展/strong>

extension UILabel {
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0, length: attributedString.length))
attributedText = attributedString
}
}
}

最新更新