Scale home view cells to reflect dynamic type size.

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

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

@ -15,16 +15,11 @@
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 ()
@property (nonatomic) AvatarImageView *avatarView;
@property (nonatomic) UIView *payloadView;
@property (nonatomic) UIStackView *topRowView;
@property (nonatomic) UILabel *nameLabel;
@property (nonatomic) UILabel *snippetLabel;
@property (nonatomic) UILabel *dateTimeLabel;
@ -76,16 +71,16 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.avatarView = [[AvatarImageView alloc] init];
[self.contentView addSubview:self.avatarView];
[self.avatarView autoSetDimension:ALDimensionWidth toSize:kHomeViewAvatarSize];
[self.avatarView autoSetDimension:ALDimensionHeight toSize:kHomeViewAvatarSize];
[self.avatarView autoPinLeadingToSuperviewMarginWithInset:kHomeViewCellHMargin];
[self.avatarView autoSetDimension:ALDimensionWidth toSize:self.avatarSize];
[self.avatarView autoSetDimension:ALDimensionHeight toSize:self.avatarSize];
[self.avatarView autoPinLeadingToSuperviewMarginWithInset:self.cellHMargin];
[self.avatarView autoVCenterInSuperview];
[self.avatarView setContentHuggingHigh];
[self.avatarView setCompressionResistanceHigh];
self.payloadView = [UIView containerView];
[self.contentView addSubview:self.payloadView];
[self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:kHomeViewAvatarHSpacing];
[self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:self.avatarHSpacing];
[self.payloadView autoVCenterInSuperview];
self.nameLabel = [UILabel new];
@ -102,6 +97,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.nameLabel,
self.dateTimeLabel,
]];
self.topRowView = topRowView;
topRowView.axis = UILayoutConstraintAxisHorizontal;
topRowView.spacing = 4;
[self.payloadView addSubview:topRowView];
@ -117,7 +113,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
[self.snippetLabel autoPinLeadingToSuperviewMargin];
[self.snippetLabel autoPinTrailingToSuperviewMargin];
[self.snippetLabel autoPinBottomToSuperviewMargin];
[self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topRowView withOffset:5.f];
[self.snippetLabel setContentHuggingHorizontalLow];
[self.snippetLabel setCompressionResistanceHorizontalLow];
@ -130,7 +125,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.unreadBadge = [NeverClearView new];
self.unreadBadge.backgroundColor = [UIColor ows_materialBlueColor];
[self.contentView addSubview:self.unreadBadge];
[self.unreadBadge autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin];
[self.unreadBadge autoPinTrailingToSuperviewMarginWithInset:self.cellHMargin];
[self.unreadBadge autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.dateTimeLabel];
[self.unreadBadge setContentHuggingHigh];
[self.unreadBadge setCompressionResistanceHigh];
@ -145,11 +140,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
return NSStringFromClass([self class]);
}
+ (CGFloat)rowHeight
{
return kHomeViewCellHeight;
}
- (void)initializeLayout
{
self.selectionStyle = UITableViewCellSelectionStyleDefault;
@ -163,15 +153,12 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
- (void)configureWithThread:(TSThread *)thread
contactsManager:(OWSContactsManager *)contactsManager
blockedPhoneNumberSet:(NSSet<NSString *> *)blockedPhoneNumberSet
shouldHaveBottomSeparator:(BOOL)shouldHaveBottomSeparator
{
OWSAssertIsOnMainThread();
OWSAssert(thread);
OWSAssert(contactsManager);
OWSAssert(blockedPhoneNumberSet);
// TODO: Honor shouldHaveBottomSeparator.
self.thread = thread;
self.contactsManager = contactsManager;
@ -190,9 +177,10 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
self.snippetLabel.attributedText =
[self attributedSnippetForThread:thread blockedPhoneNumberSet:blockedPhoneNumberSet];
self.dateTimeLabel.attributedText = [self attributedStringForDate:thread.lastMessageDate];
self.separatorInset
= UIEdgeInsetsMake(0, kHomeViewAvatarSize + kHomeViewCellHMargin + kHomeViewAvatarHSpacing, 0, 0);
CGFloat snippetHSpacing = ceil([HomeViewCell scaleValueWithDynamicType:6]);
[self.viewConstraints addObjectsFromArray:@[
[self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.topRowView withOffset:snippetHSpacing],
]];
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);
self.unreadBadge.layer.cornerRadius = unreadBadgeSize / 2;
CGFloat unreadBadgeHSpacing = ceil([HomeViewCell scaleValueWithDynamicType:7]);
[self.viewConstraints addObjectsFromArray:@[
[self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize],
[self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize],
[self.unreadBadge autoPinLeadingToTrailingEdgeOfView:self.payloadView offset:4.f],
[self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize],
[self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize],
[self.unreadBadge autoPinLeadingToTrailingEdgeOfView:self.payloadView offset:unreadBadgeHSpacing],
]];
} else {
self.unreadBadge.hidden = YES;
[self.viewConstraints addObjectsFromArray:@[
[self.payloadView autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin],
[self.payloadView autoPinTrailingToSuperviewMarginWithInset:self.cellHMargin],
]];
}
}
@ -240,7 +230,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
}
self.avatarView.image =
[OWSAvatarBuilder buildImageForThread:thread diameter:kHomeViewAvatarSize contactsManager:contactsManager];
[OWSAvatarBuilder buildImageForThread:thread diameter:self.avatarSize contactsManager:contactsManager];
}
- (NSAttributedString *)attributedSnippetForThread:(TSThread *)thread
@ -345,6 +335,38 @@ const NSUInteger kHomeViewAvatarHSpacing = 12;
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
- (void)prepareForReuse

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

@ -276,7 +276,7 @@ NS_ASSUME_NONNULL_BEGIN
cell.accessoryMessage = NSLocalizedString(
@"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked.");
} else {
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else {
// 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
@ -27,6 +27,12 @@ static inline CGFloat CGFloatInverseLerp(CGFloat value, CGFloat minValue, CGFloa
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);
NS_ASSUME_NONNULL_END

Loading…
Cancel
Save