Underline button text in Swift

后端 未结 14 1833
暖寄归人
暖寄归人 2021-01-30 10:26

I have UIButton. In interface builder I set its title to be \'Attributed\'. How can I make its title to be underlined from code in Swift?

@IBOutlet weak var myBt         


        
相关标签:
14条回答
  • 2021-01-30 10:37

    A modified version of @shlomo-koppel answer for button title, It will work if you set/change button title programmatically (like in my case I used localization)

    extension UIButton {
        func underline() {
            guard let text = self.currentTitle else { return }
            let attributedString = NSMutableAttributedString(string: text)
            attributedString.addAttribute(NSAttributedString.Key.underlineColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
            self.setAttributedTitle(attributedString, for: .normal)
        }
    }
    
    0 讨论(0)
  • 2021-01-30 10:38

    Based on some of previous answers I decide to make a class that can be easy implemented into your apps

    Swift 4

    import UIKit
    
    class UnderlineTextButton: UIButton {
    
    override func setTitle(_ title: String?, for state: UIControlState) {
        super.setTitle(title, for: .normal)
        self.setAttributedTitle(self.attributedString(), for: .normal)
    }
    
    private func attributedString() -> NSAttributedString? {
        let attributes : [NSAttributedStringKey : Any] = [
            NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19.0),
            NSAttributedStringKey.foregroundColor : UIColor.red,
            NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
        ]
        let attributedString = NSAttributedString(string: self.currentTitle!, attributes: attributes)
        return attributedString
      }
    }
    

    From code I call it on such a way button.setTitle(author, for: .normal)

    0 讨论(0)
  • 2021-01-30 10:40

    StoryBoard: If you want to Underline text from storyBoard.

    • Select button or label title as Attributed.
    • Select range of text which you want to underline.
    • Right click and choose Font then select underline.

    0 讨论(0)
  • 2021-01-30 10:42

    Swift 5 / Xcode 11

      @IBOutlet weak var myButton: UIButton!
    
      let yourAttributes: [NSAttributedString.Key: Any] = [
          .font: UIFont.systemFont(ofSize: 14),
          .foregroundColor: UIColor.blue,
          .underlineStyle: NSUnderlineStyle.single.rawValue]
             //.double.rawValue, .thick.rawValue
    
      override func viewDidLoad() {
         super.viewDidLoad()
    
         let attributeString = NSMutableAttributedString(string: "Your button text",
                                                         attributes: yourAttributes)
         myButton.setAttributedTitle(attributeString, for: .normal)
      }
    

    Swift 4 / Xcode 9

      @IBOutlet weak var myButton: UIButton!
    
      let yourAttributes : [NSAttributedStringKey: Any] = [
          NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14),
          NSAttributedStringKey.foregroundColor : UIColor.blue,
          NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue]
             //.styleDouble.rawValue, .styleThick.rawValue, .styleNone.rawValue
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        let attributeString = NSMutableAttributedString(string: "Your button text",
                                                        attributes: yourAttributes)
        myButton.setAttributedTitle(attributeString, for: .normal)
      }
    

    Swift 3 / Xcode 8

      @IBOutlet weak var myButton: UIButton!
    
      let yourAttributes : [String: Any] = [
          NSFontAttributeName : UIFont.systemFont(ofSize: 14),
          NSForegroundColorAttributeName : UIColor.white,
          NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue] 
             //.styleDouble.rawValue, .styleThick.rawValue, .styleNone.rawValue
    
       override func viewDidLoad() {
          super.viewDidLoad()
    
          let attributeString = NSMutableAttributedString(string: "Your button text", 
                                                           attributes: yourAttributes)        
          myButton.setAttributedTitle(attributeString, for: .normal) 
        }
    

    0 讨论(0)
  • 2021-01-30 10:49

    @ShlomoKoppel answer in Swift 4.2

    extension UIButton {
        func underline() {
            guard let text = self.titleLabel?.text else { return }
            let attributedString = NSMutableAttributedString(string: text)
            //NSAttributedStringKey.foregroundColor : UIColor.blue
            attributedString.addAttribute(NSAttributedString.Key.underlineColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: self.titleColor(for: .normal)!, range: NSRange(location: 0, length: text.count))
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
            self.setAttributedTitle(attributedString, for: .normal)
        }
    }
    
    
    
    extension UILabel {
        func underlineMyText() {
            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 - 1))
                attributedText = attributedString
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 10:50

    Thanks for posting your code, it wasn't clear that you knew how to create an attributed string at all.

    This should work:

    var attrs = [
        NSFontAttributeName : UIFont.systemFontOfSize(19.0),
        NSForegroundColorAttributeName : UIColor.redColor(),
        NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleSingle.rawValue
    ]
    

    Swift 4 version:

    var attrs : [NSAttributedStringKey : Any] = [
        NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19.0),
        NSAttributedStringKey.foregroundColor : UIColor.red,
        NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
    ]
    
    0 讨论(0)
提交回复
热议问题