From a0dc5950fc826f90db0775f84be0538248f82f07 Mon Sep 17 00:00:00 2001 From: Igor Ranieri Elland Date: Thu, 17 Nov 2016 23:07:18 +0100 Subject: [PATCH] Automatically adjust message bubble text size on dynamic text changes. (#1469) * Automatically adjust message bubble text size on dynamic text changes. - Addresses part of #1453. * Renamed dynamic text notification handler method. --- .../view controllers/MessagesViewController.m | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Signal/src/view controllers/MessagesViewController.m b/Signal/src/view controllers/MessagesViewController.m index 1168fc9eb..bf628e3ff 100644 --- a/Signal/src/view controllers/MessagesViewController.m +++ b/Signal/src/view controllers/MessagesViewController.m @@ -312,6 +312,10 @@ typedef enum : NSUInteger { - (void)toggleObservers:(BOOL)shouldObserve { if (shouldObserve) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(didChangePreferredContentSize:) + name:UIContentSizeCategoryDidChangeNotification + object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yapDatabaseModified:) name:YapDatabaseModifiedNotification @@ -329,6 +333,9 @@ typedef enum : NSUInteger { name:UIApplicationDidEnterBackgroundNotification object:nil]; } else { + [[NSNotificationCenter defaultCenter] removeObserver:self + name:UIContentSizeCategoryDidChangeNotification + object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:YapDatabaseModifiedNotification object:nil]; @@ -918,6 +925,47 @@ typedef enum : NSUInteger { #pragma mark - Adjusting cell label heights + +/** + Due to the usage of JSQMessagesViewController, and it non-conformity to Dynamyc Type + we're left to our own devices to make this as usable as possible. + JSQMessagesVC also does not expose the constraint for the input toolbar height nor does it seem to + give us a method to tell it to re-adjust (I think it should observe the preferredDefaultHeight property). + + With that in mind, we use magical runtime to get that property, and if it doesn't exist, we just don't apply the dynamic + type change. If it does exist, than we apply the font changes and adjust the views to contain them properly. + + This is not the prettiest code, but it's working code. We should tag this code for deletion as soon as JSQMessagesVC adops Dynamic type. + */ +- (void)reloadInputToolbarSizeIfNeeded { + NSLayoutConstraint *heightConstraint = ((NSLayoutConstraint *)[self valueForKeyPath:@"toolbarHeightConstraint"]); + if (heightConstraint == nil) { + return; + } + + [self.inputToolbar.contentView.textView setFont:[UIFont ows_dynamicTypeBodyFont]]; + + CGRect f = self.inputToolbar.contentView.textView.frame; + f.size.height = [self.inputToolbar.contentView.textView sizeThatFits:self.inputToolbar.contentView.textView.frame.size].height; + self.inputToolbar.contentView.textView.frame = f; + + self.inputToolbar.preferredDefaultHeight = self.inputToolbar.contentView.textView.frame.size.height + 16; + heightConstraint.constant = self.inputToolbar.preferredDefaultHeight; + [self.inputToolbar setNeedsLayout]; +} + + +/** + Called whenever the user manually changes the dynamic type options inside Settings. + + @param notification NSNotification with the dynamic type change information. + */ +- (void)didChangePreferredContentSize:(NSNotification *)notification { + [self.collectionView.collectionViewLayout setMessageBubbleFont:[UIFont ows_dynamicTypeBodyFont]]; + [self.collectionView reloadData]; + [self reloadInputToolbarSizeIfNeeded]; +} + - (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath {