Question

I am trying to size a UILabel to fit the bounds of an NSString, but I'm seeing differing results in iOS 7.0.3 and iOS 7.1.1. As you can see below, iOS 7.0.3 doesn't seem to draw the text correctly.

Example 1: The text is drawn toward the bottom of the label, and almost outside of the bounds:

enter image description here

Example 2: The text is drawn on 1 line (instead of 2), and no word wrapping occurs, only tail truncation.

enter image description here

Here's the code that I am using for both versions of iOS listed above.

CGSize boundingSize = CGSizeMake(214, 9999);
CGRect boundingRect = CGRectZero;
[self.nameLabel setNumberOfLines:2];

// for iOS7
if([self.place.placeName respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

    boundingRect = [self.place.placeName boundingRectWithSize:boundingSize
                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                   attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                               self.nameLabel.font, NSFontAttributeName,
                                                               paragraphStyle, NSParagraphStyleAttributeName, nil]
                                                      context:nil];

}else{
    // pre iOS7
    CGSize size = [self.place.placeName sizeWithFont:self.nameLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];
    boundingRect = CGRectMake(0, 0, size.width, size.height);
}

[self.nameLabel setFrame:CGRectMake(CGRectGetMaxX(self.photoImageView.frame)+15,
                                    CGRectGetMinY(self.photoImageView.frame),
                                    boundingRect.size.width, boundingRect.size.height)];

[self.nameLabel setText:[place placeName]];

Any ideas? Thanks in advance.

Was it helpful?

Solution

I've contacted Apple regarding this issue and they have confirmed it is a bug with iOS 7.0.3, which has been fixed in iOS 7.1.1.

The workaround for the issue (I used to use) is to create your own UILabel subclass, override the - (void)drawTextInRect: method, and use NSString’s - (void)drawInRect:withAttributes: to render the string. (Apple Developer)

OTHER TIPS

Just guessing. From the screenshot it looks, like the shorter label is word wrapped, which would indicate that the width turned out too short. It seems the implementation of the call to boundingRectWithSize might have changed in some subtle way from one iOS version to the other. It could have to do with dynamic fonts as well, who knows.

Here are a couple of things you should try/check:

  • make sure the numberOfLines property is set to 0 (or at least a high enough number to allow line wrapping, i.e. not 1.
  • give the boundingRect the maximum allowed width.
  • check if auto layout constraints are changing the frame of your label at some later stage

use the numberOfLines property is set to at least a high enough number to allow line wrapping, i.e. labelheight/20(change size and test).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top