Commit e946ce87 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Update layout of NTP article cells

Remove the extra space at bottom of NTP article cells if no image is
displayed.

Screenshots:
normal-font-top:
https://drive.google.com/open?id=11r17WFEdLeN2nr5lXAbidq0d6Pfa-2g4
normal-font-bottom:
https://drive.google.com/open?id=1Ss-R_HLKEN5I8HpP4vdWDl3FeCm47CyK
a11y-font-top:
https://drive.google.com/open?id=1IS1_NoZKGBDEmU2jsVNWY6e6VBD4jkkr
a11y-font-bottom:
https://drive.google.com/open?id=1bTPAeXJPjR8qHpeP5Z_5MAaziUypbj8J

Bug: 1003693
Change-Id: I3e38560a28636850b41400a5099f3f2ad320b11d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1889652Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710754}
parent 3ee8e8bd
...@@ -10,6 +10,43 @@ ...@@ -10,6 +10,43 @@
@class FaviconView; @class FaviconView;
// Corresponding cell for an article in the suggestions. // Corresponding cell for an article in the suggestions.
//
// a11y font size with image:
// +--------------------------------+
// | Image |
// | Image |
// | Image |
// | Title |
// | Favicon AdditionalInfo |
// +--------------------------------+
//
// a11y font size without image:
// +--------------------------------+
// | Title |
// | Favicon AdditionalInfo |
// +--------------------------------+
//
// Regular font size with image:
// +--------------------------------+
// | Title Image |
// | Image |
// | Favicon AdditionalInfo Image |
// +--------------------------------+
//
// Regular font size with image and a long title:
// +--------------------------------+
// | A very very very very Image |
// | very very very very very Image |
// | very very very very very Image |
// | very very long title |
// | Favicon AdditionalInfo |
// +--------------------------------+
//
// Regular font size without image:
// +--------------------------------+
// | Title |
// | Favicon AdditionalInfo |
// +--------------------------------+
@interface ContentSuggestionsCell : MDCCollectionViewCell @interface ContentSuggestionsCell : MDCCollectionViewCell
@property(nonatomic, readonly, strong) UILabel* titleLabel; @property(nonatomic, readonly, strong) UILabel* titleLabel;
......
...@@ -62,9 +62,6 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -62,9 +62,6 @@ const CGFloat kAnimationDuration = 0.3;
@property(nonatomic, strong) @property(nonatomic, strong)
NSArray<NSLayoutConstraint*>* accessibilityConstraints; NSArray<NSLayoutConstraint*>* accessibilityConstraints;
// Applies the constraints on the elements. Called in the init.
- (void)applyConstraints;
@end @end
@implementation ContentSuggestionsCell @implementation ContentSuggestionsCell
...@@ -169,28 +166,40 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -169,28 +166,40 @@ const CGFloat kAnimationDuration = 0.3;
title:(NSString*)title title:(NSString*)title
publisherName:(NSString*)publisherName publisherName:(NSString*)publisherName
publicationDate:(NSString*)publicationDate { publicationDate:(NSString*)publicationDate {
// Calculate title height.
UILabel* titleLabel = [[UILabel alloc] init]; UILabel* titleLabel = [[UILabel alloc] init];
[self configureTitleLabel:titleLabel]; [self configureTitleLabel:titleLabel];
titleLabel.text = title; titleLabel.text = title;
CGSize spaceForTitle = CGSizeMake(
[self availableWidthForTitleInWidth:width withImage:hasImage], 500);
CGFloat titleHeight = [titleLabel sizeThatFits:spaceForTitle].height;
// Calculate subtitle height.
UILabel* additionalInfoLabel = [[UILabel alloc] init]; UILabel* additionalInfoLabel = [[UILabel alloc] init];
additionalInfoLabel.font = [self additionalInformationFont]; additionalInfoLabel.font = [self additionalInformationFont];
additionalInfoLabel.text = additionalInfoLabel.text =
[self stringForPublisher:publisherName date:publicationDate]; [self stringForPublisher:publisherName date:publicationDate];
CGSize spaceForAdditionalInfo = CGSizeMake(
CGSize sizeForLabels = [self availableWidthForAdditionalInfoInWidth:width withImage:hasImage],
CGSizeMake(width - [self labelHorizontalMarginsWithImage:hasImage], 500); 500);
CGFloat minimalHeight = kImageSize + kStandardSpacing;
CGFloat labelHeight = [titleLabel sizeThatFits:sizeForLabels].height;
labelHeight += [self labelVerticalMarginsWithImage:hasImage];
labelHeight += kSmallSpacing;
CGFloat additionalInfoHeight = CGFloat additionalInfoHeight =
[additionalInfoLabel sizeThatFits:sizeForLabels].height; [additionalInfoLabel sizeThatFits:spaceForAdditionalInfo].height;
labelHeight += MAX(additionalInfoHeight, kFaviconSize); // The height of the line containing the favicon and the additional info.
// Add extra space at the bottom for separators between cells. CGFloat subtitleHeight = MAX(kFaviconSize, additionalInfoHeight);
return MAX(minimalHeight, labelHeight) + kStandardSpacing;
// Calculate final height. Add up heights from top to bottom.
BOOL isCurrentCategoryAccessibility =
UIContentSizeCategoryIsAccessibilityCategory(
UIApplication.sharedApplication.preferredContentSizeCategory);
if (isCurrentCategoryAccessibility || !hasImage) {
return kStandardSpacing + (hasImage ? kImageSize + kStandardSpacing : 0) +
titleHeight + kSmallSpacing + subtitleHeight + kStandardSpacing;
} else {
CGFloat leftSideHeight = kStandardSpacing + titleHeight + kSmallSpacing +
subtitleHeight + kStandardSpacing;
CGFloat rightSideHeight = kStandardSpacing + kImageSize + kStandardSpacing;
return MAX(leftSideHeight, rightSideHeight);
}
} }
#pragma mark - UICollectionViewCell #pragma mark - UICollectionViewCell
...@@ -238,10 +247,11 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -238,10 +247,11 @@ const CGFloat kAnimationDuration = 0.3;
CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds);
self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.preferredMaxLayoutWidth =
parentWidth - [[self class] availableWidthForTitleInWidth:parentWidth
[[self class] labelHorizontalMarginsWithImage:self.displayImage]; withImage:self.displayImage];
self.additionalInformationLabel.preferredMaxLayoutWidth = self.additionalInformationLabel.preferredMaxLayoutWidth =
parentWidth - kFaviconSize - kSmallSpacing - 2 * kStandardSpacing; [[self class] availableWidthForAdditionalInfoInWidth:parentWidth
withImage:self.displayImage];
// Re-layout with the new preferred width to allow the label to adjust its // Re-layout with the new preferred width to allow the label to adjust its
// height. // height.
...@@ -250,6 +260,7 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -250,6 +260,7 @@ const CGFloat kAnimationDuration = 0.3;
#pragma mark - Private #pragma mark - Private
// Applies the constraints on the elements. Called in the init.
- (void)applyConstraints { - (void)applyConstraints {
_imageSizeConstraint = _imageSizeConstraint =
[_imageContainer.heightAnchor constraintEqualToConstant:kImageSize]; [_imageContainer.heightAnchor constraintEqualToConstant:kImageSize];
...@@ -266,14 +277,14 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -266,14 +277,14 @@ const CGFloat kAnimationDuration = 0.3;
// Favicon. // Favicon.
[_faviconView.leadingAnchor [_faviconView.leadingAnchor
constraintEqualToAnchor:_titleLabel.leadingAnchor], constraintEqualToAnchor:_titleLabel.leadingAnchor],
[_faviconView.centerYAnchor
constraintEqualToAnchor:_additionalInformationLabel.centerYAnchor],
[_faviconView.heightAnchor constraintEqualToConstant:kFaviconSize], [_faviconView.heightAnchor constraintEqualToConstant:kFaviconSize],
[_faviconView.widthAnchor [_faviconView.widthAnchor
constraintEqualToAnchor:_faviconView.heightAnchor], constraintEqualToAnchor:_faviconView.heightAnchor],
[_faviconView.topAnchor [_faviconView.topAnchor
constraintGreaterThanOrEqualToAnchor:_titleLabel.bottomAnchor constraintGreaterThanOrEqualToAnchor:_titleLabel.bottomAnchor
constant:kSmallSpacing], constant:kSmallSpacing],
[_faviconView.bottomAnchor
constraintGreaterThanOrEqualToAnchor:_imageContainer.bottomAnchor],
// Additional Information. // Additional Information.
[_additionalInformationLabel.leadingAnchor [_additionalInformationLabel.leadingAnchor
...@@ -281,6 +292,15 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -281,6 +292,15 @@ const CGFloat kAnimationDuration = 0.3;
constant:kSmallSpacing], constant:kSmallSpacing],
[_additionalInformationLabel.trailingAnchor [_additionalInformationLabel.trailingAnchor
constraintEqualToAnchor:_titleLabel.trailingAnchor], constraintEqualToAnchor:_titleLabel.trailingAnchor],
[_additionalInformationLabel.topAnchor
constraintGreaterThanOrEqualToAnchor:_titleLabel.bottomAnchor
constant:kSmallSpacing],
[_additionalInformationLabel.bottomAnchor
constraintGreaterThanOrEqualToAnchor:_imageContainer.bottomAnchor],
// Align favicon with additional information.
[_additionalInformationLabel.centerYAnchor
constraintEqualToAnchor:_faviconView.centerYAnchor],
// No image icon. // No image icon.
[_noImageIcon.centerXAnchor [_noImageIcon.centerXAnchor
...@@ -293,17 +313,10 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -293,17 +313,10 @@ const CGFloat kAnimationDuration = 0.3;
AddSameConstraints(_contentImageView, _imageContainer); AddSameConstraints(_contentImageView, _imageContainer);
// Prevent _additionalInformationLabel from overlapping with _titleLabel when // Constraints for regular font size.
// a11y font size is used.
[_additionalInformationLabel.topAnchor
constraintGreaterThanOrEqualToAnchor:_titleLabel.bottomAnchor
constant:kSmallSpacing]
.active = YES;
_imageTitleHorizontalSpacing = [_imageContainer.leadingAnchor _imageTitleHorizontalSpacing = [_imageContainer.leadingAnchor
constraintEqualToAnchor:_titleLabel.trailingAnchor constraintEqualToAnchor:_titleLabel.trailingAnchor
constant:kStandardSpacing]; constant:kStandardSpacing];
_standardConstraints = @[ _standardConstraints = @[
_imageTitleHorizontalSpacing, _imageTitleHorizontalSpacing,
[_titleLabel.topAnchor constraintEqualToAnchor:_imageContainer.topAnchor], [_titleLabel.topAnchor constraintEqualToAnchor:_imageContainer.topAnchor],
...@@ -315,7 +328,7 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -315,7 +328,7 @@ const CGFloat kAnimationDuration = 0.3;
constant:-kStandardSpacing], constant:-kStandardSpacing],
]; ];
// For a11y layout, the image container is always aligned to the left border. // Constraints for a11y font size.
_imageTitleVerticalSpacing = [_titleLabel.topAnchor _imageTitleVerticalSpacing = [_titleLabel.topAnchor
constraintEqualToAnchor:_imageContainer.bottomAnchor constraintEqualToAnchor:_imageContainer.bottomAnchor
constant:kStandardSpacing]; constant:kStandardSpacing];
...@@ -358,30 +371,28 @@ const CGFloat kAnimationDuration = 0.3; ...@@ -358,30 +371,28 @@ const CGFloat kAnimationDuration = 0.3;
return [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]; return [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
} }
// Returns the horizontal margin for the labels, depending if the cell // Returns the available width for main title within |width|.
// |hasImage| and the content size category. + (CGFloat)availableWidthForTitleInWidth:(CGFloat)width
+ (CGFloat)labelHorizontalMarginsWithImage:(BOOL)hasImage { withImage:(BOOL)hasImage {
BOOL isCurrentCategoryAccessibility = BOOL isCurrentCategoryAccessibility =
UIContentSizeCategoryIsAccessibilityCategory( UIContentSizeCategoryIsAccessibilityCategory(
[UIApplication sharedApplication].preferredContentSizeCategory); UIApplication.sharedApplication.preferredContentSizeCategory);
// If |hasImage|==YES and current font size is not a11y, the image will be
CGFloat offset = (hasImage && !isCurrentCategoryAccessibility) // displayed in the same vertical level with main title. Subtract the image's
? kImageSize + kStandardSpacing // width and the margin between it and the main title.
: 0; width -= (hasImage && !isCurrentCategoryAccessibility)
return 2 * kStandardSpacing + offset; ? kImageSize + kStandardSpacing
: 0;
// Subtract the margin on both sides.
return width - 2 * kStandardSpacing;
} }
// Returns the vertical margin for the labels, depending if the cell |hasImage| + (CGFloat)availableWidthForAdditionalInfoInWidth:(CGFloat)width
// and the content size category. withImage:(BOOL)hasImage {
+ (CGFloat)labelVerticalMarginsWithImage:(BOOL)hasImage { width = [self availableWidthForTitleInWidth:width withImage:hasImage];
BOOL isCurrentCategoryAccessibility = // Subtract the favicon's width and the margin between it and the
UIContentSizeCategoryIsAccessibilityCategory( // AdditionalInfo.
[UIApplication sharedApplication].preferredContentSizeCategory); return width - kFaviconSize - kSmallSpacing;
CGFloat offset = (hasImage && isCurrentCategoryAccessibility)
? kImageSize + kStandardSpacing
: 0;
return kStandardSpacing + offset;
} }
// Returns the attributed string to be displayed. // Returns the attributed string to be displayed.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment