blob: 648cf07dcfebebab3be6a79a70c34c3a7c3feeff [file] [log] [blame] [edit]
// Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "MDCActionSheetHeaderView.h"
#import <MaterialComponents/MaterialMath.h>
#import <MaterialComponents/MaterialTypography.h>
static const CGFloat kTitleLabelAlpha = (CGFloat)0.87;
static const CGFloat kMessageLabelAlpha = (CGFloat)0.6;
static const CGFloat kMessageOnlyPadding = 23;
static const CGFloat kLeadingPadding = 0;
static const CGFloat kTopStandardPadding = 16;
static const CGFloat kTrailingPadding = 0;
static const CGFloat kTitleOnlyPadding = 18;
static const CGFloat kMiddlePadding = 8;
/** The minimum height a header can be. */
static const CGFloat kMinimumHeaderHeight = 8;
@interface MDCActionSheetHeaderView ()
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *messageLabel;
@end
@implementation MDCActionSheetHeaderView
@synthesize adjustsFontForContentSizeCategory = _adjustsFontForContentSizeCategory;
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.preservesSuperviewLayoutMargins = YES;
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:_titleLabel];
_titleLabel.font = [UIFont mdc_standardFontForMaterialTextStyle:MDCFontTextStyleSubheadline];
_titleLabel.numberOfLines = 0;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
_titleLabel.accessibilityTraits |= UIAccessibilityTraitHeader;
[self addSubview:_messageLabel];
_messageLabel.font = [UIFont mdc_standardFontForMaterialTextStyle:MDCFontTextStyleBody1];
_messageLabel.numberOfLines = 2;
_messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
_messageLabel.textColor = [UIColor.blackColor colorWithAlphaComponent:kMessageLabelAlpha];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = CGRectInfinite.size;
size.width = CGRectGetWidth(self.bounds);
CGRect labelFrame = [self frameWithLayoutMargins:self.bounds];
labelFrame = CGRectStandardize(labelFrame);
labelFrame.size.width = labelFrame.size.width - kLeadingPadding - kTrailingPadding;
CGSize titleSize = [self.titleLabel sizeThatFits:labelFrame.size];
CGSize messageSize = [self.messageLabel sizeThatFits:labelFrame.size];
CGRect titleFrame = CGRectMake(kLeadingPadding + labelFrame.origin.x, kTopStandardPadding,
labelFrame.size.width, titleSize.height);
CGRect messageFrame =
CGRectMake(kLeadingPadding + labelFrame.origin.x, CGRectGetMaxY(titleFrame) + kMiddlePadding,
labelFrame.size.width, messageSize.height);
self.titleLabel.frame = titleFrame;
self.messageLabel.frame = messageFrame;
}
- (CGSize)sizeThatFits:(CGSize)size {
size.width = size.width - kLeadingPadding - kTrailingPadding;
CGSize titleSize = [self.titleLabel sizeThatFits:size];
CGSize messageSize = [self.messageLabel sizeThatFits:size];
CGFloat contentHeight;
BOOL messageExist = (self.message) && (![self.message isEqualToString:@""]);
BOOL titleExist = (self.title) && (![self.title isEqualToString:@""]);
if (titleExist && messageExist) {
contentHeight =
titleSize.height + messageSize.height + (kTopStandardPadding * 2) + kMiddlePadding;
} else if (messageExist) {
contentHeight = messageSize.height + (kMessageOnlyPadding * 2);
} else if (titleExist) {
contentHeight = titleSize.height + (kTitleOnlyPadding * 2);
} else {
contentHeight = kMinimumHeaderHeight;
}
CGSize contentSize;
contentSize.width = ceil(size.width);
contentSize.height = ceil(contentHeight);
return contentSize;
}
- (CGRect)frameWithLayoutMargins:(CGRect)frame {
return UIEdgeInsetsInsetRect(frame, self.layoutMargins);
}
- (void)setTitle:(NSString *)title {
self.titleLabel.text = title;
[self setNeedsLayout];
}
- (NSString *)title {
return self.titleLabel.text;
}
- (void)setMessage:(NSString *)message {
self.messageLabel.text = message;
[self updateLabelColors];
[self setNeedsLayout];
}
- (NSString *)message {
return self.messageLabel.text;
}
- (void)setTitleFont:(UIFont *)titleFont {
self.titleLabel.font = titleFont;
[self updateTitleFont];
}
- (UIFont *)titleFont {
return self.titleLabel.font;
}
- (void)setMessageFont:(UIFont *)messageFont {
self.messageLabel.font = messageFont;
[self updateMessageFont];
}
- (UIFont *)messageFont {
return self.messageLabel.font;
}
- (void)updateFonts {
[self updateTitleFont];
[self updateMessageFont];
}
- (void)updateTitleFont {
UIFont *titleFont =
self.titleFont ?: [UIFont mdc_standardFontForMaterialTextStyle:MDCFontTextStyleSubheadline];
self.titleLabel.font = titleFont;
[self setNeedsLayout];
}
- (void)updateMessageFont {
UIFont *messageFont =
self.messageFont ?: [UIFont mdc_standardFontForMaterialTextStyle:MDCFontTextStyleBody1];
self.messageLabel.font = messageFont;
[self setNeedsLayout];
}
- (void)setAdjustsFontForContentSizeCategory:(BOOL)adjustsFontForContentSizeCategory {
_adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory;
self.titleLabel.adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory;
self.messageLabel.adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory;
}
- (UIColor *)defaultTitleTextColor {
// If message is empty or nil then the title label's alpha value should be lighter, if there is
// both then the title label's alpha should be darker.
if (self.message && ![self.message isEqualToString:@""]) {
return [UIColor.blackColor colorWithAlphaComponent:kTitleLabelAlpha];
} else {
return [UIColor.blackColor colorWithAlphaComponent:kMessageLabelAlpha];
}
}
- (void)updateLabelColors {
self.titleLabel.textColor = self.titleTextColor ?: [self defaultTitleTextColor];
self.messageLabel.textColor =
self.messageTextColor ?: [UIColor.blackColor colorWithAlphaComponent:kMessageLabelAlpha];
}
- (void)setTitleTextColor:(UIColor *)titleTextColor {
_titleTextColor = titleTextColor;
[self updateLabelColors];
}
- (void)setMessageTextColor:(UIColor *)messageTextColor {
_messageTextColor = messageTextColor;
[self updateLabelColors];
}
@end