Scenario: I merely want to able to fit a string within a UILabel per target device.
That is, on one iPad model the string is truncated. On another it isn't.
I suspect the application is more code-centric vs IB.
So I want to be able to work with the UILabel programmatically.
Here's the code:
class ViewController: UIViewController {
@IBOutlet var myLabel: UILabel!
@IBAction func addMessageAction(_ sender: UIBarButtonItem) {
let msg = "Mother had a feeling, I might be too appealing. But I can understand."
myLabel.text = msg
myLabel.numberOfLines = 0
myLabel.adjustsFontForContentSizeCategory = true
myLabel.sizeToFit()
myLabel.lineBreakMode = .byTruncatingTail
myLabel.minimumScaleFactor = 0.5
}
@IBAction func exitAction(_ sender: UIBarButtonItem) {
exit(0)
}
}
Here's the screen after I pressed the 'message' button:
This only appear to work with I set the UILabel properties within IB.
Why doesn't it work via code?
I tried your code and its working perfectly. Is there any Autolayout constraint mismatch ?
You can do one thing to this change the font size to fit the width of the label
myLabel.adjustsFontSizeToFitWidth = true