4

I calculate the size of a UILabel (only height matters here) by dynamic length text. And I draw the label's layer's border to visualize the frame of the label. I see "padding" above and under the label text sometimes, but not always. I do not want the padding. I suspect it relates to attributed string, since I never encounter such problem in a "normal" string label.

I see this (Note the padding of the first row):

enter image description here

I want this:

enter image description here

Relevant code:

-(void)setupQuestionView
{
    [self.questionView setAttributedText:[self.allContents[_itemIndex] objectForKey:@"Question"]];

    // new question view height
    CGSize constraint = CGSizeMake(kCellWidth - kLeftMargin - kRightMargin, FLT_MAX);

    CGSize size = [self.questionView.text sizeWithFont:[UIFont systemFontOfSize: kFontSize] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    [self.questionView setFrame:CGRectMake(kRightMargin, kTopMargin, kCellWidth - kRightMargin * 2, size.height)];

    [self.questionView.layer setBorderWidth:1.0f];      // debug
}
Philip007
  • 3,190
  • 7
  • 46
  • 71
  • I think this post may be useful for you http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel – Saurabh Shukla Jan 30 '13 at 09:47
  • Thanks. I just read the post. It offers some solutions for handling multiline label (I use one of which, see above code), yet it doesn't address my issue particularly. My question is why padding exists in my 1st screenshot while it should not. I think I have solved vertical alignment by "wrapping" label text inside a bounding box and move the box for positioning. I just can't figure out why there is padding INSIDE the bounding box SOMETIMES, which only happens when I use attributed text of the label. – Philip007 Jan 30 '13 at 15:07

1 Answers1

1

Are kLeftMargin and kRightMargin equal? I couldn't find anywhere else can possibly go wrong.

Cable W
  • 633
  • 1
  • 8
  • 17
  • Better use `constraint.width` next time:) k prefix constants are not easy for my eyes. I might need more practice. – Philip007 Feb 05 '13 at 16:29