How to set top-left alignment for UILabel for iOS application?

后端 未结 21 1382
傲寒
傲寒 2020-12-04 07:54

I have added one label in my nib file, then its required to have top-left alignment for that lable. As I am providing text at runtime so its not sure that how much lines the

相关标签:
21条回答
  • 2020-12-04 08:36

    you can also just change your UILabel to UITextView, because they basically do the same thing except the advantage of UITextView is that text is automatically aligned to the top left

    0 讨论(0)
  • 2020-12-04 08:37

    In my case, it was bottom space constraint issue. I had set it to = 16.

    When I set it to bottom to >= 16, this issue got solved.

    Also, if you have any height constraint in the label, then you need to remove it.

    Here's my label's constraint view in the size inspector:

    0 讨论(0)
  • 2020-12-04 08:37

    @totiG's answer is correct and solved my problem. But I found a problem while implementing this method, in smaller devices like 5s , SE, this doesn't work for me. I have to set label.sizeToFit() in override func layoutSubViews()

    override func layoutSubViews() {
        super.layoutSubViews()
        // Do other works if needed
        label.sizeToFit()
    }
    
    0 讨论(0)
提交回复
热议问题