I am wanting to create a label programmatically that is able to word wrap for long sentences. Here is the loop I have for creating the labels:
for i in 0..<servicesLength
{
let label = UILabel(frame: CGRectMake(0, number, servicesScroll.bounds.size.width - 42, 25))
label.lineBreakMode = .ByWordWrapping
label.numberOfLines = 0
label.font = UIFont(name: label.font.fontName, size: 25)
label.text = servicesList[i]
self.servicesScroll.addSubview(label)
number = number + 50
}
This doesnt work though. From what I've read, in order for the wrapping to work, you need to either not set the height or make the height large enough for the content. I have to set the height for the frame and I don't want to make it extremely large for large sentences so how can I get this to work by setting everything programmatically?
Why not using Autolayout, this will also let you support the orientation change on the devices.
var lastLabel:UILabel?
for i in 0..<10servicesLength
{
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints=false
label.lineBreakMode = .ByWordWrapping
label.numberOfLines = 10
label.font = UIFont(name: label.font.fontName, size: 25)
label.text = servicesList[i]
servicesScroll.addSubview(label)
let metrics=["horizontalMargin":21,"top":0,"bottom":0,"separationBetweenLabels":0,"labelMinHeight":25]
let views=["label":label]
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-horizontalMargin-[label]-horizontalMargin-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
if i == 0{
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-top-[label]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
}
if let lastLabel=lastLabel{
views["lastLabel"]=lastLabel
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[lastLabel]-separationBetweenLabels-[label]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
}
if i == (servicesLength-1){
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label]-bottom-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
}
//Optional a minimum height of 25
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label(>=labelMinHeight)]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
lastLabel=label
}
The important part is that you set the numberOfLines of each label as the maximum size that you will allow the label to grow.
Also I add a constraint for a miminum height:
//Optional a minimum height of 25
servicesScroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label(25)]",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: metrics,
views: views))
This will guarantee that the label will be at least 25 height and maximum of 10 lines in this example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With