Reputation: 85
Completely new to Swift.
I want to have my text above the "Get Started" button, but to show all the text. In the original code, I have the content where I'd like it, but I can't it to show the entire label text. What edits can make to this so that it shows the entirety of the text?
With original code:
func setupViews() {
self.view.addSubview(lblTitle)
lblTitle.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
lblTitle.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
lblTitle.widthAnchor.constraint(equalToConstant: 250).isActive=true
lblTitle.heightAnchor.constraint(equalToConstant: 80).isActive=true
self.view.addSubview(btnGetStarted)
btnGetStarted.heightAnchor.constraint(equalToConstant: 50).isActive=true
btnGetStarted.widthAnchor.constraint(equalToConstant: 150).isActive=true
btnGetStarted.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
btnGetStarted.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
}
let lblTitle: UILabel = {
let lbl=UILabel()
lbl.text="Bacon ipsum dolor amet shankle filet mignon bacon shank frankfurter buffalo. Swine andouille burgdoggen jerky. Kevin meatball jerky tri-tip tail, biltong meatloaf fatback cupim pork chop."
lbl.textColor=UIColor.darkGray
lbl.textAlignment = .center
lbl.font = UIFont.systemFont(ofSize: 46)
lbl.numberOfLines=2
lbl.translatesAutoresizingMaskIntoConstraints=false
return lbl
}()
let btnGetStarted: UIButton = {
let btn=UIButton()
btn.setTitle("Get Started", for: .normal)
btn.setTitleColor(UIColor.white, for: .normal)
btn.backgroundColor=UIColor.orange
btn.layer.cornerRadius=5
btn.layer.masksToBounds=true
btn.translatesAutoresizingMaskIntoConstraints=false
btn.addTarget(self, action: #selector(btnGetStartedAction), for: .touchUpInside)
return btn
}()
}
Upvotes: 1
Views: 875
Reputation: 4393
Both the label and the button have minimumScaleFactor property as well as a Bool property to fit width. Try using that.
button.titleLabel?.minimumScaleFactor = 0.1
button.titleLabel?.numberOfLines = 2
button.titleLabel?.adjustsFontSizeToFitWidth = true
The label has the same properties to scale the font.
lbl.minimumScaleFactor = 0.1
lbl.numberOfLines = 2
lbl.adjustsFontSizeToFitWidth = true
Upvotes: 2
Reputation: 9652
height
of the label and add topAnchor
to the button Add adjustsFontSizeToFitWidth
, numberOfLines=0
, sizeToFit()
to the label
func setupViews() {
self.view.addSubview(lblTitle)
lblTitle.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
lblTitle.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
lblTitle.widthAnchor.constraint(equalToConstant: 250).isActive=true
lblTitle.heightAnchor.constraint(equalToConstant: 250).isActive=true
self.view.addSubview(btnGetStarted)
btnGetStarted.topAnchor.constraint(equalTo: lblTitle.bottomAnchor, constant: 20).isActive=true
btnGetStarted.heightAnchor.constraint(equalToConstant: 50).isActive=true
btnGetStarted.widthAnchor.constraint(equalToConstant: 150).isActive=true
btnGetStarted.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
btnGetStarted.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
}
let lblTitle: UILabel = {
let lbl=UILabel()
lbl.text="Bacon ipsum dolor amet shankle filet mignon bacon shank frankfurter buffalo. Swine andouille burgdoggen jerky. Kevin meatball jerky tri-tip tail, biltong meatloaf fatback cupim pork chop."
lbl.textColor=UIColor.darkGray
lbl.textAlignment = .center
lbl.font = UIFont.systemFont(ofSize: 46)
lbl.adjustsFontSizeToFitWidth = true
lbl.numberOfLines=0
lbl.sizeToFit()
lbl.translatesAutoresizingMaskIntoConstraints=false
return lbl
}()
Upvotes: 0