constraints and multilined labels

Discussion in 'iOS Programming' started by Chirone, Feb 12, 2013.

  1. Chirone, Feb 12, 2013
    Last edited: Feb 12, 2013

    Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    I'm doing this all in code so I don't have to do the same thing twice.

    I have two labels and a button.
    I want one label to always be 20 points from the top
    I want the button to always be 20 points from the bottom
    I want the other label to always be 20 points down from the bottom of the label at the top and to be multilined with word wrap.
    The problem is that I won't know how big the multilined label should be as the text will keep changing.

    How do I use constraints to keep the title from resizing?
    If I use the visual constraint:
    Code:
    :@"V:|-[titleLabel]-[descriptionLabel]-[startButton]-|"
    then the descriptionLabel doesn't change height. The titleLabel does.
    I can put in a value for the descriptionLabel's height but I don't know what that should be.

    I know in the old days I could just call sizeToFit on descriptionLabel to make the right height to fit all the text but I don't know what you do for the constraints.

    Anyone got any ideas?

    I would also like to add that
    Code:
    V:|-[titleLabel]-[descriptionLabel(>=20)]-(>=20)-[startButton]-|
    gets me a large gap between descriptionLabel and startButton but descriptionLabel doesn't change height to fit the text.

    Code:
    V:|-[titleLabel]-[descriptionLabel(>=20)]-[startButton]-|
    will change the titleLabel's height. I don't know how to restrict it to not change size when I don't know what size it is.
     
  2. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #2
    Wait, nevermind.
    While I was looking for something in the UILabel docs I found the preferredMaxLayoutWidth property. Just had to set that and the constraint with the variable gap between descriptionLabel and startButton and it works the way I want it to.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    As an aside, you could use a UITextView that is non-editable. Just set it the max possible height and it will display the text at the top. The inability to flush text to the top of a label is often a pain.
     
  4. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #4
    Indeed I could have and it actually probably wouldn't have been different visually.
     

Share This Page