import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Main View"
//Button looks like Clickable Text Link
addButton1()
}
func addButton1(){
let button1 = UIButton()
button1.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(button1)
button1.translatesAutoresizingMaskIntoConstraints = false
let attributedString = NSMutableAttributedString.init(string: "This is a BUTTON")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
NSRange.init(location: 0, length: attributedString.length))
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range:
NSRange.init(location: 0, length: attributedString.length))
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 24.0), range: NSRange.init(location: 0, length: attributedString.length))
button1.setAttributedTitle(attributedString, for: .normal)
var allConstraints: [NSLayoutConstraint] = []
let views = ["view": view!, "button1": button1]
let horizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-[button1]-(>=10)-|", metrics: nil, views: views)
allConstraints += horizontalConstraints
let verticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-(>=40)-[button1]", metrics: nil, views: views)
allConstraints += verticalConstraints
NSLayoutConstraint.activate(allConstraints)
button1.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
@objc func buttonAction(sender: UIButton!) {
if let buttonTitle = sender.titleLabel?.text {
print(buttonTitle + ": button tapped!")
}
}
}
All one can think and do in a short time is to think what one already knows and to do as one has always done!
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.