Reputation: 39
When I change the text attribute of my UILabel, it only partially updates in the app.
I've tried putting it in the viewDidLoad function and in a separate dedicated function to no avail. If I print the Label.text to the console after the update it is correct, but it doesn't show up properly on screen.
The class in question:
@IBOutlet weak var PromptLabel: UILabel!
var labelText = "";
override func viewDidLoad() {
super.viewDidLoad()
print("view loaded")
self.PromptLabel.text = "Tell Us... " + labelText;
// Do any additional setup after loading the view.
}
Called by:
@IBAction func UsingButtonPressed(_ sender: Any) {
(sender as AnyObject).setTitleColor(UIColor.red, for: .normal)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let secondController = storyBoard.instantiateViewController(withIdentifier: "Secondary View Controller") as! SecondaryViewController;
secondController.labelText = "What You're Using Today"
self.present(secondController, animated: true, completion: nil)
}
The label should display as "Tell Us... What You're Using Today" but it only shows "Tell U..." which makes no sense.
Upvotes: 1
Views: 125
Reputation: 3064
You need to make sure that the constraint for the UILabel
's width is wide enough to accommodate the text. Otherwise, it is being cutoff and the ...
you are seeing is because the label's line break is set to truncating tail
, which means that the end of the string will be replaced with ...
when the label is too narrow.
So this is an interface builder issue and not a Swift-specific issue. The label's text is being correctly updated, the UI is just not able to properly display it.
If you want the width constraint of the label to change dependent on the text, there are ways to calculate the width of the text and update the constraint's constant to accommodate that text's width.
Upvotes: 2