Constraining the area for text in an UILabelView

Discussion in 'iOS Programming' started by gwelmarten, Jul 11, 2012.

  1. gwelmarten macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #1
    Hi
    I've got a UILabelView that takes up a large amount of space (and it has to do this).
    However, I want the text within it to stay within a small area within it. I've had a look around, and I see there is a method fro containing the area text can occupy in a text view - is there something similar for UILabelView? So I can keep text within a CGRect of the label view?
    Thanks for your help,
    Sam
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Why does it have to take up a large amount of space?
     
  3. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #3
    Because of an animation I'm running on it. I have thought about resizing it, but no-one in my team can think of a way of then handling the animation.
    So is there a way of handling a constraint? I couldn't see one in the class reference, so I presume no.

    ----------

    Just realised I was looking in the Mac class reference - in the iOS one, I saw:
    textRectForBounds:limitedToNumberOfLines:
    I think something like this will work, so I'm going to try. Any views on if it will?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No idea! Give it a try and report back.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What kind of animation? Have you considered animating a UIView and having the UILabel (there is no UILabelView, by the way) as a subview?
     
  6. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #6
    Hmm...no luck. To create the UILabelView, I've got:
    Code:
        question = [[UILabel alloc] init];
        question.frame = CGRectMake(0, 0, 480, 300);
        question.textAlignment = UITextAlignmentCenter;
        question.text = @"Loading...";
        question.lineBreakMode = UILineBreakModeWordWrap;
        question.numberOfLines = 4; //Or whatever you want
        question.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"x019xm2.png"]];
        [question drawTextInRect:CGRectMake(100, 100, 50, 50)];
        [self.view addSubview:question];
    Now it hasn't worked, I quickly googled drawTextInRect and others seem to have similar code. Is the CGRect still based on the screen size - not the size of the UILabelView?

    Sam

    ----------

    Hi Dejo
    Yes, we did, and we tried it. And no, it didn't work for what we are trying to do.
    Sam

    ----------

    Oh...just read the rest of the class reference, and that method is only for subclassing.
    So it won't work in the way I was trying.
    I'll have to try and find another way. If I do, I'll post it back here for your interest.
    Sam
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Again, I ask: what kind of animation?
     

Share This Page