iOS Swift how to Underline a Label text

iOS Swift how to Underline a Label text


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "Main View"
        
        //Underline text in a UILabel
        addLabel()
        
        
   }
    
    func addLabel(){
        
        let myLabel = UILabel()
        myLabel.textColor = UIColor.black
        myLabel.numberOfLines = 0;
        myLabel.font = UIFont(name: "Marker Felt", size: 24)
        myLabel.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myLabel)
        
        let attributedString = NSMutableAttributedString.init(string: "UILabel UnderLining")
        attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
            NSRange.init(location: 0, length: attributedString.length))
        myLabel.attributedText = attributedString
        
        var allConstraints: [NSLayoutConstraint] = []
        let views = ["view": view!, "myLabel": myLabel]
        
        let horizontalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-[myLabel]-(>=10)-|", metrics: nil, views: views)
        allConstraints += horizontalConstraints
        
        let verticalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-(>=40)-[myLabel]", metrics: nil, views: views)
        allConstraints += verticalConstraints
        
        NSLayoutConstraint.activate(allConstraints)
 
    }
    

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.