Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using iOS 7 text size methods, when deploying to iOS 7+.

- circumvents deprication warnings
  • Loading branch information...
commit a50f7a36a4d4e37fc804aa50b17f0a3163d92957 1 parent 355651d
@matej matej authored
Showing with 18 additions and 3 deletions.
  1. +18 −3 MBProgressHUD.m
View
21 MBProgressHUD.m
@@ -23,6 +23,22 @@
#define MBLabelAlignmentCenter UITextAlignmentCenter
#endif
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
+ #define MB_TEXTSIZE(text, font) [text length] > 0 ? [text \
+ sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero;
+#else
+ #define MB_TEXTSIZE(text, font) [text sizeWithFont:font];
+#endif
+
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
+ #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \
+ boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \
+ attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;
+#else
+ #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text sizeWithFont:font \
+ constrainedToSize:maxSize lineBreakMode:mode];
+#endif
+
static const CGFloat kPadding = 4.f;
static const CGFloat kLabelFontSize = 16.f;
@@ -514,7 +530,7 @@ - (void)layoutSubviews {
totalSize.width = MAX(totalSize.width, indicatorF.size.width);
totalSize.height += indicatorF.size.height;
- CGSize labelSize = [label.text sizeWithFont:label.font];
+ CGSize labelSize = MB_TEXTSIZE(label.text, label.font);
labelSize.width = MIN(labelSize.width, maxWidth);
totalSize.width = MAX(totalSize.width, labelSize.width);
totalSize.height += labelSize.height;
@@ -524,8 +540,7 @@ - (void)layoutSubviews {
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
- CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
- constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
+ CGSize detailsLabelSize = MB_MULTILINE_TEXTSIZE(detailsLabel.text, detailsLabel.font, maxSize, detailsLabel.lineBreakMode);
totalSize.width = MAX(totalSize.width, detailsLabelSize.width);
totalSize.height += detailsLabelSize.height;
if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {

7 comments on commit a50f7a3

@elbryan

We should check whether an empty string returns 0 height, as it was returning pre-iOS 7.

Specifically:

NSLog(@"%@", NSStringFromCGSize([@"" sizeWithFont:[UIFont systemFontOfSize:14]]));

  • (on iOS <7): {0, 0}
  • (on iOS ≥7): {0, 17}

Oh, and:

NSLog(@"%@", NSStringFromCGSize([@"" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0]}])) -> {0, 16.309999}

In iOS 7, you will get a bigger vertical margin compared to iOS pre-7.

@matej
Collaborator

An empty string ([text length] == 0) would return CGSizeZero on iOS 7 using MB_TEXTSIZE, wouldn't it?

@elbryan

Sorry, I think I'm tired. Ultimately I'm missing stuff. Didn't notice the short conditional.

This is good. Elb-approved :+1:

@elbryan

Sorry but this is not properly working. As said, on iOS 7 the behavior of the old sizeWithFont has changed. So, your code is not working properly (returns height > 0) if the hud will be displayed on a iOS7 device and [text lenght] == 0. This means that such conditional switch against the text length should be performed also in the #else branch of the macro.

@elbryan

As a side note, using a nil pointer instead of @"" returns 0 height.

@matej
Collaborator

You are right. Should be fixed in e7b7493.

On a side note, I'm perhaps adding the height when the string is empty @"" is in fact the better way to handle this. Like you said, you could always set the string to nil, if you don't want that extra space.

@kautaleksei

Just for notice, after you calculate size by this method: boundingRectWithSize:maxSize options (NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:font} context:nil].size

You must use ceilf function, to works properly. From official docs:

This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
Please sign in to comment.
Something went wrong with that request. Please try again.