Underline button text in Swift

后端 未结 14 1832
暖寄归人
暖寄归人 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:26

    Here you go, just tested it. (works in xCode 7 Beta at least)

    @IBOutlet weak var yourButton: UIButton!
    
    var attrs = [
    NSFontAttributeName : UIFont.systemFontOfSize(19.0),
    NSForegroundColorAttributeName : UIColor.redColor(),
    NSUnderlineStyleAttributeName : 1]
    
    var attributedString = NSMutableAttributedString(string:"")
    
    override func viewDidLoad() {
      super.viewDidLoad()
    
      let buttonTitleStr = NSMutableAttributedString(string:"My Button", attributes:attrs)
      attributedString.appendAttributedString(buttonTitleStr)
      yourButton.setAttributedTitle(attributedString, forState: .Normal)
    }
    
    0 讨论(0)
  • 2021-01-30 10:30

    For swift 5

    var attrs : [NSAttributedString.Key : Any] = [
        NSAttributedString.Key.font : UIFont.systemFont(ofSize: 19.0),
        NSAttributedString.Key.foregroundColor : UIColor.blue,
        NSAttributedString.Key.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
    ]
    
    0 讨论(0)
  • 2021-01-30 10:31

    Here is done on the storyboard. (Xcode 9.1)

    1. Select the Button object in your view.
    2. Open Fonts Settings

    1. Select Single Underline

    1. Type your text, press [Enter]
    0 讨论(0)
  • 2021-01-30 10:32

    This is my solution. And to be honest you probably need this more than one place, so let's create an extension. This is swift 5.0 Cheers :)

    extension UIButton {
        func underline() {
            guard let title = self.titleLabel else { return }
            guard let tittleText = title.text else { return }
            let attributedString = NSMutableAttributedString(string: (tittleText))
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: (tittleText.count)))
            self.setAttributedTitle(attributedString, for: .normal)
        }
    }
    

    And you can use it like this.

        override func viewDidLoad() {
         super.viewDidLoad()
         button.underline()
    }
    
    0 讨论(0)
  • 2021-01-30 10:33
    • Swift 5.2.4
    • Xcode 11.5
    let attributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.underlineStyle: 1,
    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13),
    NSAttributedString.Key.foregroundColor: UIColor.systemGray3
    ]
    
    let attributedString = NSMutableAttributedString(string: "Text here", attributes: attributes)
    button.setAttributedTitle(NSAttributedString(attributedString: attributedString), for: .normal)
    
    0 讨论(0)
  • 2021-01-30 10:35

    if you are looking for a way to do this without inheritance -

    swift 3/4/5

    // in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyle
    
    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 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 - 1))
                attributedText = attributedString
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题