如何在swift中强调UILabel?

如何在Swift中强调UILabel ? 我search了Objective-C的,但不能让他们在Swift中工作。

你可以使用NSAttributedString来做到这一点

例:

 let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue] let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute) myLabel.attributedText = underlineAttributedString 

编辑

要为一个UILabel的所有文本具有相同的属性,我build议你inheritanceUILabel并重写文本,如下所示:

Swift 3.0

 class UnderlinedLabel: UILabel { override var text: String? { didSet { guard let text = text else { return } let textRange = NSMakeRange(0, text.characters.count) let attributedText = NSMutableAttributedString(string: text) attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange) // Add other attributes if needed self.attributedText = attributedText } } } 

而你把你的文字如下所示:

 @IBOutlet weak var label: UnderlinedLabel! override func viewDidLoad() { super.viewDidLoad() label.text = "StringWithUnderLine" } 

旧:

Swift(2.0到2.3):

 class UnderlinedLabel: UILabel { override var text: String? { didSet { guard let text = text else { return } let textRange = NSMakeRange(0, text.characters.count) let attributedText = NSMutableAttributedString(string: text) attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange) // Add other attributes if needed self.attributedText = attributedText } } } 

Swift 1.2:

 class UnderlinedLabel: UILabel { override var text: String! { didSet { let textRange = NSMakeRange(0, count(text)) let attributedText = NSMutableAttributedString(string: text) attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange) // Add other attributes if needed self.attributedText = attributedText } } } 

一class轮快3:

 label.attributedText = NSAttributedString(string: "Text", attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]) 

您可以使用Interface Builder强调UILabel文本。

这是我的答案的链接: 添加下划线属性的部分文字UILabel故事板

如果你正在寻找一种没有inheritance的方法 –

迅速3/4

 // in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyle extension UILabel { func underline() { if let textString = self.text { let attributedString = NSMutableAttributedString(string: textString) attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length - 1)) attributedText = attributedString } } } extension UIButton { func underline() { let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!) attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!)) self.setAttributedTitle(attributedString, for: .normal) } } 

只是Swift 4Xcode 9中的Shlome答案的一点点修复。

 extension UILabel { func underline() { if let textString = self.text { let attributedString = NSMutableAttributedString(string: textString) attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length - 1)) attributedText = attributedString } } } extension UIButton { func underline() { let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!) attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!)) self.setAttributedTitle(attributedString, for: .normal) } } 

斯威夫特4变化。 记住使用NSUnderlineStyle.styleSingle.rawValue而不是NSUnderlineStyle.styleSingle

  'let attributedString = NSAttributedString(string: "Testing") let textRange = NSMakeRange(0, attributedString.length) let underlinedMessage = NSMutableAttributedString(attributedString: attributedString) underlinedMessage.addAttribute(NSAttributedStringKey.underlineStyle, value:NSUnderlineStyle.styleSingle.rawValue, range: textRange) label.attributedText = underlinedMessage 

`

上面的答案在我的构build环境中导致错误。

这在Swift 4.0中不起作用:

 attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange) 

试试这个:

 attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange) 

希望这可以帮助别人。