Skip to content

Commit

Permalink
[Tabs] Expose the the iconImageView and the titleLabel of the item vi…
Browse files Browse the repository at this point in the history
…ew (#7703)

Instead of using title and image as public property, exposing the image view and the label will be more convenient.
  • Loading branch information
mikefan1991 authored and Robert Moore committed Jun 25, 2019
1 parent 966cee2 commit 1008ad3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 66 deletions.
4 changes: 2 additions & 2 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];
// TODO(#7645): Remove this if autoresizing masks are used.
itemView.translatesAutoresizingMaskIntoConstraints = NO;
itemView.title = item.title;
itemView.image = item.image;
itemView.titleLabel.text = item.title;
itemView.iconImageView.image = item.image;
[itemViews addObject:itemView];
[self addSubview:itemView];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
/** A basic view that displays a title and image for a tab bar item within MDCTabBarView. */
@interface MDCTabBarViewItemView : UIView

/** Title for the tab. Defaults to the empty string. */
@property(nonatomic, copy, nullable) NSString *title;
/** The image view to display the icon. */
@property(nonatomic, strong) UIImageView *iconImageView;

/** Image shown on the tab. Defaults to nil. */
@property(nonatomic, strong, nullable) UIImage *image;
/** The label to display the title. */
@property(nonatomic, strong) UILabel *titleLabel;

@end
23 changes: 3 additions & 20 deletions components/Tabs/src/TabBarView/private/MDCTabBarViewItemView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
@interface MDCTabBarViewItemView ()

@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) UILabel *titleLabel;

@end

Expand All @@ -44,8 +42,6 @@ @implementation MDCTabBarViewItemView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_title = @"";

self.isAccessibilityElement = YES;

// Create initial subviews
Expand All @@ -57,8 +53,6 @@ - (instancetype)initWithFrame:(CGRect)frame {
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_title = @"";

self.isAccessibilityElement = YES;

// Create initial subviews
Expand Down Expand Up @@ -113,8 +107,9 @@ - (CGSize)intrinsicContentSize {
}

- (CGSize)sizeThatFits:(CGSize)size {
const CGFloat minHeight =
(self.title && self.image) ? kMinHeightTitleAndImage : kMinHeightTitleOrImageOnly;
NSString *title = self.titleLabel.text;
UIImage *icon = self.iconImageView.image;
const CGFloat minHeight = (title && icon) ? kMinHeightTitleAndImage : kMinHeightTitleOrImageOnly;
CGFloat horizontalPadding = kEdgeInsets.left + kEdgeInsets.right;
CGFloat verticalPadding = kEdgeInsets.top + kEdgeInsets.bottom;
// The size of the content view should be smaller that the size passed in.
Expand All @@ -135,16 +130,4 @@ - (CGSize)sizeThatFits:(CGSize)size {

#pragma mark - Public API

- (void)setTitle:(NSString *)title {
_title = [title copy];
self.titleLabel.text = _title;
[self setNeedsLayout];
}

- (void)setImage:(nullable UIImage *)image {
_image = image;
self.iconImageView.image = _image;
[self setNeedsLayout];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ - (void)setUp {
// self.recordMode = YES;

self.itemView = [[MDCTabBarViewItemView alloc] init];
self.itemView.title = kShortTitleLatin;
self.itemView.titleLabel.text = kShortTitleLatin;
// Default to white since in actual use the background would be transparent.
self.itemView.backgroundColor = UIColor.whiteColor;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(24, 24)
withStyle:MDCSnapshotTestImageStyleFramedX];
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(24, 24) withStyle:MDCSnapshotTestImageStyleFramedX];
}

- (void)tearDown {
Expand All @@ -79,8 +79,8 @@ - (void)changeToRTL {

- (void)testNoTitleNoImageIntrinsicContentSize {
// When
self.itemView.title = nil;
self.itemView.image = nil;
self.itemView.titleLabel.text = nil;
self.itemView.iconImageView.image = nil;
CGSize intrinsicContentSize = self.itemView.intrinsicContentSize;
self.itemView.bounds = CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height);

Expand All @@ -99,7 +99,7 @@ - (void)testShortTitleRegularImageIntrinsicContentSizeLTRLatin {

- (void)testShortTitleRegularImageIntrinsicContentSizeRTLArabic {
// When
self.itemView.title = kShortTitleArabic;
self.itemView.titleLabel.text = kShortTitleArabic;
[self changeToRTL];
CGSize intrinsicContentSize = self.itemView.intrinsicContentSize;
self.itemView.bounds = CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height);
Expand All @@ -110,9 +110,10 @@ - (void)testShortTitleRegularImageIntrinsicContentSizeRTLArabic {

- (void)testLongTitleLargeImageIntrinsicContentSizeLTRLatin {
// When
self.itemView.title = kLongTitleLatin;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleLatin;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
CGSize intrinsicContentSize = self.itemView.intrinsicContentSize;
self.itemView.bounds = CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height);

Expand All @@ -122,9 +123,10 @@ - (void)testLongTitleLargeImageIntrinsicContentSizeLTRLatin {

- (void)testLongTitleLargeImageIntrinsicContentSizeRTLArabic {
// When
self.itemView.title = kLongTitleArabic;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleArabic;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
[self changeToRTL];
CGSize intrinsicContentSize = self.itemView.intrinsicContentSize;
self.itemView.bounds = CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height);
Expand All @@ -135,9 +137,10 @@ - (void)testLongTitleLargeImageIntrinsicContentSizeRTLArabic {

- (void)testLongTitleLargeImageTooSmallSizeLTRLatin {
// When
self.itemView.title = kLongTitleLatin;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleLatin;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.bounds = CGRectMake(0, 0, 36, 36);

// Then
Expand All @@ -146,9 +149,10 @@ - (void)testLongTitleLargeImageTooSmallSizeLTRLatin {

- (void)testLongTitleLargeImageTooSmallSizeRTLArabic {
// When
self.itemView.title = kLongTitleArabic;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleArabic;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
[self changeToRTL];
self.itemView.bounds = CGRectMake(0, 0, 36, 40);

Expand All @@ -158,9 +162,10 @@ - (void)testLongTitleLargeImageTooSmallSizeRTLArabic {

- (void)testLongTitleLargeImageVeryTallSizeLTRLatin {
// When
self.itemView.title = kLongTitleLatin;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleLatin;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.bounds = CGRectMake(0, 0, 36, 360);

// Then
Expand All @@ -169,9 +174,10 @@ - (void)testLongTitleLargeImageVeryTallSizeLTRLatin {

- (void)testLongTitleLargeImageVeryTallSizeRTLArabic {
// When
self.itemView.title = kLongTitleArabic;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleArabic;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
[self changeToRTL];
self.itemView.bounds = CGRectMake(0, 0, 36, 360);

Expand All @@ -181,9 +187,10 @@ - (void)testLongTitleLargeImageVeryTallSizeRTLArabic {

- (void)testLongTitleLargeImageWidestSizeLTRLatin {
// When
self.itemView.title = kLongTitleLatin;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleLatin;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.bounds = CGRectMake(0, 0, 1200, 36);

// Then
Expand All @@ -192,9 +199,10 @@ - (void)testLongTitleLargeImageWidestSizeLTRLatin {

- (void)testLongTitleLargeImageWidestSizeRTLArabic {
// When
self.itemView.title = kLongTitleArabic;
self.itemView.image = [UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
self.itemView.titleLabel.text = kLongTitleArabic;
self.itemView.iconImageView.image =
[UIImage mdc_testImageOfSize:CGSizeMake(48, 48)
withStyle:MDCSnapshotTestImageStyleRectangles];
[self changeToRTL];
self.itemView.bounds = CGRectMake(0, 0, 1200, 36);

Expand Down
20 changes: 10 additions & 10 deletions components/Tabs/tests/unit/TabBarView/MDCTabBarViewItemViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ - (void)testIntrinsicContentSizeForLargeImageLimitsWidth {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.image = fakeImage(1000, 1000);
itemView.iconImageView.image = fakeImage(1000, 1000);
CGSize intrinsicContentSize = itemView.intrinsicContentSize;

// Then
Expand All @@ -79,7 +79,7 @@ - (void)testIntrinsicContentSizeForLongTitleLimitsWidth {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.title = kLongTitle;
itemView.titleLabel.text = kLongTitle;
CGSize intrinsicContentSize = itemView.intrinsicContentSize;

// Then
Expand All @@ -93,8 +93,8 @@ - (void)testIntrinsicContentSizeForShortTitleExpectedImageLimitsHeight {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.image = fakeImage(24, 24);
itemView.title = @"Favorites";
itemView.iconImageView.image = fakeImage(24, 24);
itemView.titleLabel.text = @"Favorites";
CGSize intrinsicContentSize = itemView.intrinsicContentSize;

// Then
Expand Down Expand Up @@ -150,7 +150,7 @@ - (void)testSizeThatFitsForLargeImageWithSmallerDimensionsLimitsWidth {
CGSize requestedSize = CGSizeMake(kMaxWidth, kMinHeightOfTitleOrImageOnlyView);

// When
itemView.image = fakeImage(1000, 1000);
itemView.iconImageView.image = fakeImage(1000, 1000);
CGSize fitSize = [itemView sizeThatFits:requestedSize];

// Then
Expand All @@ -164,7 +164,7 @@ - (void)testSizeThatFitsForLargeImageWithLargerDimensionsLimitsWidth {
CGSize requestedSize = CGSizeMake(2000, 2000);

// When
itemView.image = fakeImage(1000, 1000);
itemView.iconImageView.image = fakeImage(1000, 1000);
CGSize fitSize = [itemView sizeThatFits:requestedSize];

// Then
Expand All @@ -177,7 +177,7 @@ - (void)testSizeThatFitsForLongTitleWithSmallerDimensionsLimitsWidth {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.title = kLongTitle;
itemView.titleLabel.text = kLongTitle;
CGSize fitSize = [itemView sizeThatFits:CGSizeZero];

// Then
Expand All @@ -191,7 +191,7 @@ - (void)testSizeThatFitsForLongTitleWithLargerDimensionsLimitsWidth {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.title = kLongTitle;
itemView.titleLabel.text = kLongTitle;
CGSize fitSize = [itemView sizeThatFits:CGSizeMake(1000, 1000)];

// Then
Expand All @@ -207,8 +207,8 @@ - (void)testSizeThatFitsForShortTitleExpectedImageLargerDimensionsLimitsHeight {
MDCTabBarViewItemView *itemView = [[MDCTabBarViewItemView alloc] init];

// When
itemView.image = fakeImage(24, 24);
itemView.title = @"Favorites";
itemView.iconImageView.image = fakeImage(24, 24);
itemView.titleLabel.text = @"Favorites";
CGSize fitSize = [itemView sizeThatFits:CGSizeMake(1000, 1000)];

// Then
Expand Down

0 comments on commit 1008ad3

Please sign in to comment.