Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in sizeWithFont #1917

Merged
merged 3 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Frameworks/UIKit/NSString+UIKitAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,14 @@ - (CGSize)_sizeWithAttributes:(NSDictionary<NSString*, id>*)attributes constrain
size.height = std::numeric_limits<CGFloat>::max();
}

return CTFramesetterSuggestFrameSizeWithConstraints(framesetter.get(), CFRangeMake(0, self.length), nullptr, size, nullptr);
CGSize ret = CTFramesetterSuggestFrameSizeWithConstraints(framesetter.get(), CFRangeMake(0, self.length), nullptr, size, nullptr);

// If the constrained height is less than a line height sizeWithFont will increase the returned size to fit at least one line
UIFont* font = attributes[NSFontAttributeName];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there's no font?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i figured you'd just end up with a 0 size, so you'd get ret's height.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the compiler is working as intended, yes :) We had issues with returning floats from nil receivers before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like reference platform just returns CGSizeZero, will update to reflect that

CGFloat lineHeight = [font ascender] - [font descender];
ret.height = std::max(ret.height, lineHeight);

return ret;
}

// Private helper that converts a UILineBreakMode -> NSParagraphStyle
Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/UIKit/NSString+UIKitAdditionsTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@
#import <TestFramework.h>
#import <UIKit/UIKit.h>

static constexpr double c_errorDelta = .0001;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use numEquals

TEST(NSString_UIKitAdditions, ShouldNotReturnSizeOfZeroWidth) {
CGSize size =
[@"TEST" sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeZero lineBreakMode:NSLineBreakByWordWrapping];
EXPECT_LT(0.0, size.width);
EXPECT_LT(0.0, size.height);
}

TEST(NSString_UIKitAdditions, SizeWithFontShouldReturnLineHeightWhenConstrainedHeightIsTooSmall) {
CGSize givenSize = { 0, 15 };
UIFont* font = [UIFont systemFontOfSize:144];
CGFloat lineHeight = [font ascender] - [font descender];
CGSize size = [@"TEST" sizeWithFont:font constrainedToSize:givenSize];
EXPECT_NEAR(lineHeight, size.height, c_errorDelta);

size = [@"TEST\nTEST" sizeWithFont:font constrainedToSize:givenSize];
EXPECT_NEAR(lineHeight, size.height, c_errorDelta);
}