Compared with iOS14, in iOS15 boundingRectWithSize:options: attributes: context: returns a different value. We found this maybe related to the attribute NSBaselineOffsetAttributeName and the option NSStringDrawingUsesFontLeading. For example, we use the same code:
UIFont *baseFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
CGFloat baselineOffset = (29 - baseFont.lineHeight) / 4;
NSDictionary *baseTextAttributes = @{
NSFontAttributeName : baseFont,
NSParagraphStyleAttributeName : ({
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSTextAlignmentLeft];
[style setMinimumLineHeight:29];
[style setMaximumLineHeight:29];
style;
}),
NSKernAttributeName : @(0.25),
NSBaselineOffsetAttributeName : @(baselineOffset),
};
NSString *str = @"Compared with iOS14 in iOS15 boundingRectWithSize: options: attributes: context: returns a different value.";
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString: str attributes:baseTextAttributes];
CGSize size = [attributeStr boundingRectWithSize:CGSizeMake(428, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine)
context:nil]
.size;
In iPhone12 Pro Max iOS14, the return value is
(CGSize) size = (width = 421.933999999997, height = 58)
but in iOS15 the return value is
(CGSize) size = (width = 421.93399999999997, height = 54.700000000000003)
The difference between their heights happens to be equal to the baselineOffset times the number of lines.
If I remove NSBaselineOffsetAttributeName during the initialization of attributeString , or remove the option NSStringDrawingUsesFontLeading during calculation time, the calculated heights are the same in iOS 14 and 15.
Should I expect this difference between iOS14 and iOS15 or is this a bug? Thanks!