I have a textfield with lines
property set to 0, so that when the text is longer it will automatically fill all the necessary space. But when the string is short, it is automatically placed in the middle of the label, it looks like this:
Is there a way (or maybe a property to set) of putting this text in the top left corner? This is my attribute inspector for that label:
Solutions with CSS properties Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.
If you are using Autolayout you can try to set only top
, left
and right
constraints without any height constraint. That should work, but if you are not using Autolayout or you are having trouble changing these constraints you can also call label.sizeToFit()
after setting the text.
To call sizeToFit
, if you are setting the text on viewDidLoad
or in the Storyboard itself, you can call it on viewDidLayoutSubviews
:
override func viewDidLayoutSubviews() {
label.sizeToFit()
}
This really should be a default option in Xcode, you could use the work around in the other answer or alternatively just use a TextView which by default gives you the top left behaviour you're looking for
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