Skip to content

Commit

Permalink
[Tabs] Improve text and image rendering. (#7803)
Browse files Browse the repository at this point in the history
The frames for title and image were not pixel-aligned, resulting in somewhat
blurry text and images.

Part of #7748
  • Loading branch information
Robert Moore committed Jul 8, 2019
1 parent 4c41b10 commit 0dc3125
Show file tree
Hide file tree
Showing 46 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions MaterialComponentsBeta.podspec
Expand Up @@ -113,6 +113,7 @@ Pod::Spec.new do |mdc|
]
extension.dependency "MaterialComponents/AnimationTiming"
extension.dependency "MaterialComponents/Ripple"
extension.dependency "MaterialComponents/private/Math"
extension.dependency "MDFInternationalization"

extension.test_spec 'UnitTests' do |unit_tests|
Expand Down
1 change: 1 addition & 0 deletions components/Tabs/BUILD
Expand Up @@ -96,6 +96,7 @@ mdc_extension_objc_library(
deps = [
"//components/AnimationTiming",
"//components/Ripple",
"//components/private/Math",
"@material_internationalization_ios//:MDFInternationalization",
],
)
Expand Down
69 changes: 43 additions & 26 deletions components/Tabs/src/TabBarView/private/MDCTabBarViewItemView.m
Expand Up @@ -14,6 +14,10 @@

#import "MDCTabBarViewItemView.h"

#import <CoreGraphics/CoreGraphics.h>

#import "MaterialMath.h"

/** The minimum width of any item view. */
static const CGFloat kMinWidth = 90;

Expand Down Expand Up @@ -126,15 +130,16 @@ - (void)layoutSubviews {
}

- (CGRect)contentFrameForTitleLabelInTextOnlyLayout {
CGRect contentFrame = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsTextOnly);
CGRect insetBounds = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsTextOnly);

CGSize contentSize = CGSizeMake(CGRectGetWidth(contentFrame), CGRectGetHeight(contentFrame));
CGSize contentSize = CGSizeMake(CGRectGetWidth(insetBounds), CGRectGetHeight(insetBounds));
CGSize labelWidthFitSize = [self.titleLabel sizeThatFits:contentSize];
CGSize labelSize =
CGSizeMake(labelWidthFitSize.width, MIN(contentSize.height, labelWidthFitSize.height));
return CGRectMake(CGRectGetMidX(contentFrame) - (labelWidthFitSize.width / 2),
CGRectGetMidY(contentFrame) - (labelWidthFitSize.height / 2), labelSize.width,
labelSize.height);
CGRect contentFrame = CGRectMake(CGRectGetMidX(insetBounds) - (labelWidthFitSize.width / 2),
CGRectGetMidY(insetBounds) - (labelWidthFitSize.height / 2),
labelSize.width, labelSize.height);
return MDCRectAlignToScale(contentFrame, self.window.screen.scale);
}

- (void)layoutSubviewsTextOnly {
Expand All @@ -144,20 +149,23 @@ - (void)layoutSubviewsTextOnly {
CGSize labelWidthFitSize = [self.titleLabel sizeThatFits:contentSize];
CGSize labelSize =
CGSizeMake(contentSize.width, MIN(contentSize.height, labelWidthFitSize.height));
self.titleLabel.bounds = CGRectMake(0, 0, labelSize.width, labelSize.height);
self.titleLabel.center = CGPointMake(CGRectGetMidX(contentFrame), CGRectGetMidY(contentFrame));
CGRect labelFrame = CGRectMake(CGRectGetMidX(contentFrame) - (labelSize.width / 2),
CGRectGetMidY(contentFrame) - (labelSize.height / 2),
labelSize.width, labelSize.height);
self.titleLabel.frame = MDCRectAlignToScale(labelFrame, self.window.screen.scale);
}

- (CGRect)contentFrameForImageViewInImageOnlyLayout {
CGRect contentFrame = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsImageOnly);
CGRect insetBounds = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsImageOnly);

CGSize contentSize = CGSizeMake(CGRectGetWidth(contentFrame), CGRectGetHeight(contentFrame));
CGSize contentSize = CGSizeMake(CGRectGetWidth(insetBounds), CGRectGetHeight(insetBounds));
CGSize imageIntrinsicContentSize = self.iconImageView.intrinsicContentSize;
CGSize imageFinalSize = CGSizeMake(MIN(contentSize.width, imageIntrinsicContentSize.width),
MIN(contentSize.height, imageIntrinsicContentSize.height));
return CGRectMake(CGRectGetMidX(contentFrame) - (imageFinalSize.width / 2),
CGRectGetMidY(contentFrame) - (imageFinalSize.height / 2), imageFinalSize.width,
imageFinalSize.height);
CGRect contentFrame = CGRectMake(CGRectGetMidX(insetBounds) - (imageFinalSize.width / 2),
CGRectGetMidY(insetBounds) - (imageFinalSize.height / 2),
imageFinalSize.width, imageFinalSize.height);
return MDCRectAlignToScale(contentFrame, self.window.screen.scale);
}

- (void)layoutSubviewsImageOnly {
Expand All @@ -167,14 +175,16 @@ - (void)layoutSubviewsImageOnly {
CGSize imageIntrinsicContentSize = self.iconImageView.intrinsicContentSize;
CGSize imageFinalSize = CGSizeMake(MIN(contentSize.width, imageIntrinsicContentSize.width),
MIN(contentSize.height, imageIntrinsicContentSize.height));
self.iconImageView.bounds = CGRectMake(0, 0, imageFinalSize.width, imageFinalSize.height);
self.iconImageView.center = CGPointMake(CGRectGetMidX(contentFrame), CGRectGetMidY(contentFrame));
CGRect imageViewFrame = CGRectMake(CGRectGetMidX(contentFrame) - (imageFinalSize.width / 2),
CGRectGetMidY(contentFrame) - (imageFinalSize.height / 2),
imageFinalSize.width, imageFinalSize.height);
self.iconImageView.frame = MDCRectAlignToScale(imageViewFrame, self.window.screen.scale);
}

- (CGRect)contentFrameForTitleLabelInTextAndImageLayout {
CGRect contentFrame = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsTextAndImage);
CGRect insetBounds = UIEdgeInsetsInsetRect(self.bounds, kEdgeInsetsTextAndImage);

CGSize contentSize = CGSizeMake(CGRectGetWidth(contentFrame), CGRectGetHeight(contentFrame));
CGSize contentSize = CGSizeMake(CGRectGetWidth(insetBounds), CGRectGetHeight(insetBounds));
CGSize labelSingleLineSize = self.titleLabel.intrinsicContentSize;
CGSize availableIconSize = CGSizeMake(
contentSize.width, contentSize.height - (kImageTitlePadding + labelSingleLineSize.height));
Expand All @@ -189,8 +199,10 @@ - (CGRect)contentFrameForTitleLabelInTextAndImageLayout {
CGSize availableLabelSize = CGSizeMake(
contentSize.width, contentSize.height - (imageFinalSize.height + kImageTitlePadding));
CGSize finalLabelSize = [self.titleLabel sizeThatFits:availableLabelSize];
return CGRectMake(CGRectGetMidX(contentFrame) - (finalLabelSize.width / 2),
CGRectGetMinY(contentFrame), finalLabelSize.width, contentSize.height);
CGRect contentFrame =
CGRectMake(CGRectGetMidX(insetBounds) - (finalLabelSize.width / 2),
CGRectGetMinY(insetBounds), finalLabelSize.width, contentSize.height);
return MDCRectAlignToScale(contentFrame, self.window.screen.scale);
}

- (void)layoutSubviewsTextAndImage {
Expand All @@ -206,17 +218,22 @@ - (void)layoutSubviewsTextAndImage {
CGSize imageFinalSize =
CGSizeMake(MIN(imageIntrinsicContentSize.width, availableIconSize.width),
MIN(imageIntrinsicContentSize.height, availableIconSize.height));
self.iconImageView.bounds = CGRectMake(0, 0, imageFinalSize.width, imageFinalSize.height);
self.iconImageView.center = CGPointMake(CGRectGetMidX(contentFrame),
CGRectGetMinY(contentFrame) + imageFinalSize.height / 2);
CGRect imageViewFrame =
CGRectMake(CGRectGetMidX(contentFrame) - (imageFinalSize.width / 2),
CGRectGetMinY(contentFrame), imageFinalSize.width, imageFinalSize.height);
imageViewFrame = MDCRectAlignToScale(imageViewFrame, self.window.screen.scale);
self.iconImageView.frame = imageViewFrame;

// Now position the label from the bottom.
CGSize availableLabelSize = CGSizeMake(
contentSize.width, contentSize.height - (imageFinalSize.height + kImageTitlePadding));
CGSize availableLabelSize =
CGSizeMake(contentSize.width,
contentSize.height - (CGRectGetHeight(imageViewFrame) + kImageTitlePadding));
CGSize finalLabelSize = [self.titleLabel sizeThatFits:availableLabelSize];
self.titleLabel.bounds = CGRectMake(0, 0, finalLabelSize.width, finalLabelSize.height);
self.titleLabel.center = CGPointMake(CGRectGetMidX(contentFrame),
CGRectGetMaxY(contentFrame) - finalLabelSize.height / 2);
CGRect titleFrame = CGRectMake(CGRectGetMidX(contentFrame) - (finalLabelSize.width / 2),
CGRectGetMaxY(contentFrame) - finalLabelSize.height,
finalLabelSize.width, finalLabelSize.height);
titleFrame = MDCRectAlignToScale(titleFrame, self.window.screen.scale);
self.titleLabel.frame = titleFrame;
}

- (CGSize)intrinsicContentSize {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0dc3125

Please sign in to comment.