NSAttributedString boundingRectWithSize gives different heights between iOS 6 and iOS 7

I recently upgraded my application to iOS 7 using Xcode 5 and found that boundingRectWithSize gives a different height (in terms of size) that computes the bounds of attribute strings.

The following line gives me different results between iOS 6 and iOS 7:

CGRect rect = [self boundingRectWithSize:CGSizeMake(inWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil]; 

"self" is the NSAttributedString, and "inWidth" is the maximum width in pixels that the string should fit.

I think this is because iOS 7 has a different font handling than iOS 6.

Has anyone got a working solution for calculating line height on both versions of iOS?

+6
source share
2 answers

Since we cannot use sizeWithAttributes for all iOS greater than 4.3, we must write conditional code for 7.0 and previous iOS. Therefore, I suggest using this solution.

 UILabel *gettingSizeLabel = [[UILabel alloc] init]; gettingSizeLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; gettingSizeLabel.text = @"YOUR TEXT HERE"; gettingSizeLabel.numberOfLines = 0; CGSize maximumLabelSize = CGSizeMake(310, 9999); // this width will be as per your requirement CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize]; 

The option works pretty well and works smoothly in all iOS without a conditional code.

+7
source

I had the same problem, for me a simple level () at a height solved it. Also remember to set the correct attributes for the string you specify, for example.

 @{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName : label.font} 
+1
source

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


All Articles