UILabel rendering issue using boundingRectWithSize in iOS 7.0.3

I am trying to configure UILabel to comply with NSString restrictions, but I see different results in iOS 7.0.3 and iOS 7.1.1. As you can see below, iOS 7.0.3 does not seem to draw text correctly.

Example 1: text is drawn at the bottom of the label and almost beyond the borders:

enter image description here

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

enter image description here

Here is the code that I use 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.

+4
source share
3 answers

I contacted Apple on this issue and they confirmed that this is a bug with iOS 7.0.3 fixed in iOS 7.1.1.

( ) - UILabel, draw() (draw) NSStrings - (void) drawInRect: withAttributes: . ( Apple)

+3

. , , , , . -, boundingRectWithSize - iOS . , .

, /:

  • , numberOfLines 0 (, , , , .. 1.
  • boundingRect .
  • , .
0

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

0
source

Source: https://habr.com/ru/post/1539880/


All Articles