views:

181

answers:

1

See image for example: http://img25.imageshack.us/img25/6996/90754687.png
The grey background indicates the size of the UILabel frame.

For some reason, the first line of wrapped text doesn't seem to always center, even though I'm using UITextAlignmentCenter.

Here's the code I use to set up my labels:

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.font = [UIFont boldSystemFontOfSize:fontHeight];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.numberOfLines = 2;
    titleLabel.lineBreakMode = UILineBreakModeMiddleTruncation;

    NSString * title = file.name;
    CGSize maximumSize = CGSizeMake(thumbnailWidth+4,fontHeight * 3);
    UIFont * font = [UIFont boldSystemFontOfSize:12];
    CGSize stringSize = [title sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:titleLabel.lineBreakMode];

    CGRect stringFrame = CGRectMake(0, thumbnailHeight + thumbnailPadding, thumbnailWidth + 4, stringSize.height);
    titleLabel.text = title;
    titleLabel.frame = stringFrame;
    titleLabel.textAlignment = UITextAlignmentCenter;
+1  A: 

Is that because there are no spaces in the text? In IB it appears to react just like your getting, if you have no spaces. Setting the line break mode to character wrap tends to center the second lines to the first, but that may not be entirely what you want either.

v01d