Scale home view cells to reflect dynamic type size.

pull/1/head
Matthew Chen 7 years ago
parent fb27d496e6
commit c3345a4c4e

@ -15,8 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)configureWithThread:(TSThread *)thread - (void)configureWithThread:(TSThread *)thread
contactsManager:(OWSContactsManager *)contactsManager contactsManager:(OWSContactsManager *)contactsManager
blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet;
shouldHaveBottomSeparator:(BOOL)shouldHaveBottomSeparator;
@end @end

@ -15,16 +15,11 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
const NSUInteger kHomeViewCellHeight = 72;
const NSUInteger kHomeViewCellHMargin = 16;
const NSUInteger kHomeViewCellVMargin = 12;
const NSUInteger kHomeViewAvatarSize = kHomeViewCellHeight - kHomeViewCellVMargin * 2;
const NSUInteger kHomeViewAvatarHSpacing = 12;
@interface HomeViewCell () @interface HomeViewCell ()
@property (nonatomic) AvatarImageView *avatarView; @property (nonatomic) AvatarImageView *avatarView;
@property (nonatomic) UIView *payloadView; @property (nonatomic) UIView *payloadView;
@property (nonatomic) UIStackView *topRowView;
@property (nonatomic) UILabel *nameLabel; @property (nonatomic) UILabel *nameLabel;
@property (nonatomic) UILabel *snippetLabel; @property (nonatomic) UILabel *snippetLabel;
@property (nonatomic) UILabel *dateTimeLabel; @property (nonatomic) UILabel *dateTimeLabel;
@ -76,16 +71,16 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.avatarView = [[AvatarImageView alloc] init]; self.avatarView = [[AvatarImageView alloc] init];
[self.contentView addSubview:self.avatarView]; [self.contentView addSubview:self.avatarView];
[self.avatarView autoSetDimension:ALDimensionWidth toSize:kHomeViewAvatarSize]; [self.avatarView autoSetDimension:ALDimensionWidth toSize:self.avatarSize];
[self.avatarView autoSetDimension:ALDimensionHeight toSize:kHomeViewAvatarSize]; [self.avatarView autoSetDimension:ALDimensionHeight toSize:self.avatarSize];
[self.avatarView autoPinLeadingToSuperviewMarginWithInset:kHomeViewCellHMargin]; [self.avatarView autoPinLeadingToSuperviewMarginWithInset:self.cellHMargin];
[self.avatarView autoVCenterInSuperview]; [self.avatarView autoVCenterInSuperview];
[self.avatarView setContentHuggingHigh]; [self.avatarView setContentHuggingHigh];
[self.avatarView setCompressionResistanceHigh]; [self.avatarView setCompressionResistanceHigh];
self.payloadView = [UIView containerView]; self.payloadView = [UIView containerView];
[self.contentView addSubview:self.payloadView]; [self.contentView addSubview:self.payloadView];
[self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:kHomeViewAvatarHSpacing]; [self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:self.avatarHSpacing];
[self.payloadView autoVCenterInSuperview]; [self.payloadView autoVCenterInSuperview];
self.nameLabel = [UILabel new]; self.nameLabel = [UILabel new];
@ -102,6 +97,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.nameLabel, self.nameLabel,
self.dateTimeLabel, self.dateTimeLabel,
]]; ]];
self.topRowView = topRowView;
topRowView.axis = UILayoutConstraintAxisHorizontal; topRowView.axis = UILayoutConstraintAxisHorizontal;
topRowView.spacing = 4; topRowView.spacing = 4;
[self.payloadView addSubview:topRowView]; [self.payloadView addSubview:topRowView];
@ -117,7 +113,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
[self.snippetLabel autoPinLeadingToSuperviewMargin]; [self.snippetLabel autoPinLeadingToSuperviewMargin];
[self.snippetLabel autoPinTrailingToSuperviewMargin]; [self.snippetLabel autoPinTrailingToSuperviewMargin];
[self.snippetLabel autoPinBottomToSuperviewMargin]; [self.snippetLabel autoPinBottomToSuperviewMargin];
[self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topRowView withOffset:5.f];
[self.snippetLabel setContentHuggingHorizontalLow]; [self.snippetLabel setContentHuggingHorizontalLow];
[self.snippetLabel setCompressionResistanceHorizontalLow]; [self.snippetLabel setCompressionResistanceHorizontalLow];
@ -130,7 +125,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.unreadBadge = [NeverClearView new]; self.unreadBadge = [NeverClearView new];
self.unreadBadge.backgroundColor = [UIColor ows_materialBlueColor]; self.unreadBadge.backgroundColor = [UIColor ows_materialBlueColor];
[self.contentView addSubview:self.unreadBadge]; [self.contentView addSubview:self.unreadBadge];
[self.unreadBadge autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin]; [self.unreadBadge autoPinTrailingToSuperviewMarginWithInset:self.cellHMargin];
[self.unreadBadge autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.dateTimeLabel]; [self.unreadBadge autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.dateTimeLabel];
[self.unreadBadge setContentHuggingHigh]; [self.unreadBadge setContentHuggingHigh];
[self.unreadBadge setCompressionResistanceHigh]; [self.unreadBadge setCompressionResistanceHigh];
@ -145,11 +140,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
return NSStringFromClass([self class]); return NSStringFromClass([self class]);
} }
+ (CGFloat)rowHeight
{
return kHomeViewCellHeight;
}
- (void)initializeLayout - (void)initializeLayout
{ {
self.selectionStyle = UITableViewCellSelectionStyleDefault; self.selectionStyle = UITableViewCellSelectionStyleDefault;
@ -163,15 +153,12 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
- (void)configureWithThread:(TSThread *)thread - (void)configureWithThread:(TSThread *)thread
contactsManager:(OWSContactsManager *)contactsManager contactsManager:(OWSContactsManager *)contactsManager
blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet
shouldHaveBottomSeparator:(BOOL)shouldHaveBottomSeparator
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
OWSAssert(thread); OWSAssert(thread);
OWSAssert(contactsManager); OWSAssert(contactsManager);
OWSAssert(blockedPhoneNumberSet); OWSAssert(blockedPhoneNumberSet);
// TODO: Honor shouldHaveBottomSeparator.
self.thread = thread; self.thread = thread;
self.contactsManager = contactsManager; self.contactsManager = contactsManager;
@ -190,9 +177,10 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.snippetLabel.attributedText = self.snippetLabel.attributedText =
[self attributedSnippetForThread:thread blockedPhoneNumberSet:blockedPhoneNumberSet]; [self attributedSnippetForThread:thread blockedPhoneNumberSet:blockedPhoneNumberSet];
self.dateTimeLabel.attributedText = [self attributedStringForDate:thread.lastMessageDate]; self.dateTimeLabel.attributedText = [self attributedStringForDate:thread.lastMessageDate];
CGFloat snippetHSpacing = ceil([HomeViewCell scaleValueWithDynamicType:6]);
self.separatorInset [self.viewConstraints addObjectsFromArray:@[
= UIEdgeInsetsMake(0, kHomeViewAvatarSize + kHomeViewCellHMargin + kHomeViewAvatarHSpacing, 0, 0); [self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.topRowView withOffset:snippetHSpacing],
]];
self.dateTimeLabel.textColor = hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor]; self.dateTimeLabel.textColor = hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor];
@ -209,16 +197,18 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
const int unreadBadgeSize = (int)ceil(self.unreadLabel.font.lineHeight * 1.5f); const int unreadBadgeSize = (int)ceil(self.unreadLabel.font.lineHeight * 1.5f);
self.unreadBadge.layer.cornerRadius = unreadBadgeSize / 2; self.unreadBadge.layer.cornerRadius = unreadBadgeSize / 2;
CGFloat unreadBadgeHSpacing = ceil([HomeViewCell scaleValueWithDynamicType:7]);
[self.viewConstraints addObjectsFromArray:@[ [self.viewConstraints addObjectsFromArray:@[
[self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize], [self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize],
[self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize], [self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize],
[self.unreadBadge autoPinLeadingToTrailingEdgeOfView:self.payloadView offset:4.f], [self.unreadBadge autoPinLeadingToTrailingEdgeOfView:self.payloadView offset:unreadBadgeHSpacing],
]]; ]];
} else { } else {
self.unreadBadge.hidden = YES; self.unreadBadge.hidden = YES;
[self.viewConstraints addObjectsFromArray:@[ [self.viewConstraints addObjectsFromArray:@[
[self.payloadView autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin], [self.payloadView autoPinTrailingToSuperviewMarginWithInset:self.cellHMargin],
]]; ]];
} }
} }
@ -240,7 +230,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
} }
self.avatarView.image = self.avatarView.image =
[OWSAvatarBuilder buildImageForThread:thread diameter:kHomeViewAvatarSize contactsManager:contactsManager]; [OWSAvatarBuilder buildImageForThread:thread diameter:self.avatarSize contactsManager:contactsManager];
} }
- (NSAttributedString *)attributedSnippetForThread:(TSThread *)thread - (NSAttributedString *)attributedSnippetForThread:(TSThread *)thread
@ -345,6 +335,38 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
return [UIFont ows_dynamicTypeFootnoteFont]; return [UIFont ows_dynamicTypeFootnoteFont];
} }
+ (CGFloat)scaleValueWithDynamicType:(CGFloat)minValue
{
const NSUInteger kReferenceFontSizeMin = 17.f;
CGFloat referenceFontSize = UIFont.ows_dynamicTypeBodyFont.pointSize;
// We don't want to scale these values any smaller than their reference values.
CGFloat alpha = MAX(1.f, referenceFontSize / kReferenceFontSizeMin);
return minValue * alpha;
}
+ (CGFloat)rowHeight
{
const NSUInteger kMinCellHeight = 72;
return CeilEven([self scaleValueWithDynamicType:kMinCellHeight]);
}
- (NSUInteger)cellHMargin
{
return 16;
}
- (NSUInteger)avatarSize
{
return 48.f;
}
- (NSUInteger)avatarHSpacing
{
return 12.f;
}
#pragma mark - Reuse #pragma mark - Reuse
- (void)prepareForReuse - (void)prepareForReuse

@ -210,6 +210,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.delegate = self; self.tableView.delegate = self;
self.tableView.dataSource = self; self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:HomeViewCell.cellReuseIdentifier]; [self.tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:HomeViewCell.cellReuseIdentifier];
[self.view addSubview:self.tableView]; [self.view addSubview:self.tableView];
[self.tableView autoPinWidthToSuperview]; [self.tableView autoPinWidthToSuperview];
@ -583,13 +584,9 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
TSThread *thread = [self threadForIndexPath:indexPath]; TSThread *thread = [self threadForIndexPath:indexPath];
BOOL isLastCell = (indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1);
BOOL shouldHaveBottomSeparator = !isLastCell;
[cell configureWithThread:thread [cell configureWithThread:thread
contactsManager:self.contactsManager contactsManager:self.contactsManager
blockedPhoneNumberSet:self.blockedPhoneNumberSet blockedPhoneNumberSet:self.blockedPhoneNumberSet];
shouldHaveBottomSeparator:shouldHaveBottomSeparator];
if ((unsigned long)indexPath.row == [self.threadMappings numberOfItemsInSection:0] - 1) { if ((unsigned long)indexPath.row == [self.threadMappings numberOfItemsInSection:0] - 1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f); cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);

@ -276,7 +276,7 @@ NS_ASSUME_NONNULL_BEGIN
cell.accessoryMessage = NSLocalizedString( cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked."); @"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
} else { } else {
cell.selectionStyle = UITableViewCellSeparatorStyleNone; cell.selectionStyle = UITableViewCellSelectionStyleNone;
} }
} else { } else {
// In the "members" section, we label "new" members as such when editing an existing group. // In the "members" section, we label "new" members as such when editing an existing group.

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -27,6 +27,12 @@ static inline CGFloat CGFloatInverseLerp(CGFloat value, CGFloat minValue, CGFloa
return (value - minValue) / (maxValue - minValue); return (value - minValue) / (maxValue - minValue);
} }
// Ceil to an even number
static inline CGFloat CeilEven(CGFloat value)
{
return 2.f * ceil(value * 0.5f);
}
void SetRandFunctionSeed(void); void SetRandFunctionSeed(void);
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

Loading…
Cancel
Save