如何在swift中强调UILabel?

Esq*_*uth 76 uilabel ios swift

如何UILabel在Swift中强调一下?我搜索了Objective-C但却无法让它们在Swift中工作.

Ham*_*mza 189

您可以使用NSAttributedString执行此操作

例:

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

编辑

为了使一个UILabel的所有文本具有相同的属性,我建议您继承UILabel并覆盖文本,如下所示:

Swift 4.2

class UnderlinedLabel: UILabel {

override var text: String? {
    didSet {
        guard let text = text else { return }
        let textRange = NSMakeRange(0, text.count)
        let attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: textRange)
        // Add other attributes if needed
        self.attributedText = attributedText
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你把你的文字放在这样:

@IBOutlet weak var label: UnderlinedLabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = "StringWithUnderLine"
    }
Run Code Online (Sandbox Code Playgroud)

旧:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


has*_*san 89

Swift 4.2一个班轮:

label.attributedText = NSAttributedString(string: "Text", attributes:
    [.underlineStyle: NSUnderlineStyle.single.rawValue])
Run Code Online (Sandbox Code Playgroud)

Swift 4 one liner:

label.attributedText = NSAttributedString(string: "Text", attributes:
    [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
Run Code Online (Sandbox Code Playgroud)

Swift 3 one liner:

label.attributedText = NSAttributedString(string: "Text", attributes:
      [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
Run Code Online (Sandbox Code Playgroud)


Shl*_*pel 14

如果你正在寻找一种没有继承的方法 -

迅速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)
  }
}
Run Code Online (Sandbox Code Playgroud)


Ela*_*los 8

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)
        }
    }
Run Code Online (Sandbox Code Playgroud)


Riz*_*ikh 7

您可以UILabel使用Interface Builder 为文本加下划线.

这是我的答案的链接:在故事板中将下划线属性添加到部分文本UILabel


ale*_*los 7

斯威夫特4:

1- 创建 String扩展以获取attributedText.

2- 使用

延期:

import UIKit
extension String {
   func getUnderLineAttributedText() -> NSAttributedString {
       return NSMutableAttributedString(string: self, attributes: [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
   }
}
Run Code Online (Sandbox Code Playgroud)

如何在buttton上使用它:

if let title = button.titleLabel?.text{
    button.setAttributedTitle(title.getUnderLineAttributedText(), for: .normal)
}
Run Code Online (Sandbox Code Playgroud)

如何在标签上使用它:

if let title = label.text{    
   label.attributedText = title.getUnderLineAttributedText()
}
Run Code Online (Sandbox Code Playgroud)

或者 Stoyboard版本


Abd*_*ich 7

Swift 4.2中的相同答案

对于UI 可用

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: textString.count))
            self.attributedText = attributedString
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

调用UILabel如下

myLable.underline()
Run Code Online (Sandbox Code Playgroud)

对于UIButton

extension UIButton {
    func underline() {
        if let textString = self.titleLabel?.text {

            let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
                                          value: NSUnderlineStyle.single.rawValue,
                                          range: NSRange(location: 0, length: textString.count))
            self.setAttributedTitle(attributedString, for: .normal)
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

调用UIButton如下所示

myButton.underline()
Run Code Online (Sandbox Code Playgroud)

我查看了上面的答案,其中一些是强制展开文本值。我建议通过安全地拆开包装来获得价值。这将避免零值时发生崩溃。希望这可以帮助 :)


Gur*_*ngh 6

斯威夫特 4、4.2 和 5。

  @IBOutlet weak var lblUnderLine: UILabel!
Run Code Online (Sandbox Code Playgroud)

我需要在 UILabel 中的特定文本下划线。因此,找到范围并设置属性。

    let strSignup = "Don't have account? SIGNUP NOW."
    let rangeSignUp = NSString(string: strSignup).range(of: "SIGNUP NOW.", options: String.CompareOptions.caseInsensitive)
    let rangeFull = NSString(string: strSignup).range(of: strSignup, options: String.CompareOptions.caseInsensitive)
    let attrStr = NSMutableAttributedString.init(string:strSignup)
    attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
                           NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 17)! as Any],range: rangeFull)
    attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
                           NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 20)!,
                          NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue as Any],range: rangeSignUp) // for swift 4 -> Change thick to styleThick
    lblUnderLine.attributedText = attrStr
Run Code Online (Sandbox Code Playgroud)

输出

在此输入图像描述