Skip to content

Commit

Permalink
Fix TWTR Composer Height when initial text is long
Browse files Browse the repository at this point in the history
Summary: This change merges PR touren#10 (twitter-archive#10). It also fixes Issue touren#1, and PR touren#9 (links in JIRA ticket).

Note: Most changes in this file are due to the linter.

JIRA Issues: PUBPL-1666
  • Loading branch information
JaviSoto authored and Rajul Arora committed Jul 13, 2018
1 parent 246f7ee commit ba813f0
Showing 1 changed file with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#import "TWTRSETweetTextViewContainer.h"
#import "UIView+TSEExtensions.h"


#pragma mark - static const definitions

static const CGFloat kCursorRectVerticalOutsetForScroll = 4.0;
Expand All @@ -35,8 +34,7 @@

static const NSTimeInterval kScrollRetryDelay = 0.1;

static const UIEdgeInsets kComposeTextViewTextContainerInsets = { .top = 8, .left = 12, .bottom = 8, .right = 8 };

static const UIEdgeInsets kComposeTextViewTextContainerInsets = {.top = 8, .left = 12, .bottom = 8, .right = 8};

#pragma mark -

Expand Down Expand Up @@ -91,12 +89,12 @@ - (instancetype)init
_characterCounterLabel.backgroundColor = [UIColor clearColor];
_characterCounterLabel.font = [TWTRSEFonts characterCountFont];
_characterCounterLabel.numberOfLines = 1;
_characterCounterLabel.textAlignment = NSTextAlignmentRight; // varies from Apple SLShareViewController, but stays out of the way better
_characterCounterLabel.textAlignment = NSTextAlignmentRight; // varies from Apple SLShareViewController, but stays out of the way better

_characterCountOverLimitColor = [TWTRSEFonts characterCountLimitColor];
_characterCountBelowLimitColor = _placeholderLabel.textColor;

_numberOfLinesToDisplay = self.minNumberOfLinesToDisplay; // even with empty text, provide a bit of buffer
_numberOfLinesToDisplay = self.minNumberOfLinesToDisplay; // even with empty text, provide a bit of buffer

[self addSubview:_placeholderLabel];
[self addSubview:_textView];
Expand Down Expand Up @@ -171,9 +169,9 @@ - (void)addAttachmentViewConstraints
const UILayoutGuide *defaultMargins = self.layoutMarginsGuide;
const CGFloat attachmentViewPadding = kAttachmentViewPadding * (CGFloat)(TWTRSEUIIsIOS11OrGreater() ? 1 : 3);
[self.attachmentView.leadingAnchor constraintEqualToAnchor:defaultMargins.leadingAnchor constant:attachmentViewPadding].active = YES;
[self.attachmentView.topAnchor constraintEqualToAnchor:self.characterCounterLabel.bottomAnchor constant:kAttachmentViewPadding/2.0].active = YES;
[self.attachmentView.widthAnchor constraintEqualToAnchor:defaultMargins.widthAnchor constant:(CGFloat)-2.0*attachmentViewPadding].active = YES;
[self.attachmentView.bottomAnchor constraintEqualToAnchor:defaultMargins.bottomAnchor constant:(CGFloat)1.5*(kAttachmentViewPadding-attachmentViewPadding)].active = YES;
[self.attachmentView.topAnchor constraintEqualToAnchor:self.characterCounterLabel.bottomAnchor constant:kAttachmentViewPadding / 2.0].active = YES;
[self.attachmentView.widthAnchor constraintEqualToAnchor:defaultMargins.widthAnchor constant:(CGFloat)-2.0 * attachmentViewPadding].active = YES;
[self.attachmentView.bottomAnchor constraintEqualToAnchor:defaultMargins.bottomAnchor constant:(CGFloat)1.5 * (kAttachmentViewPadding - attachmentViewPadding)].active = YES;
});
}

Expand All @@ -183,6 +181,13 @@ - (void)_tseui_updateCharacterCount
self.characterCounterLabel.textColor = ([self.tweet isNearOrOverCharacterLimit]) ? _characterCountOverLimitColor : _characterCountBelowLimitColor;
}

- (void)layoutSubviews
{
[super layoutSubviews];

[self _tseui_updateLineCount];
}

- (void)_tseui_updateLineCount
{
NSUInteger textViewNumberOfLines = _textView.numberOfLines;
Expand All @@ -206,7 +211,7 @@ - (void)configureWithTweet:(TWTRSETweet *)tweet

_placeholderLabel.hidden = tweet.text.length > 0;
if ([_placeholderLabel isHidden]) {
_placeholderLabel.alpha = 0; // for later animation
_placeholderLabel.alpha = 0; // for later animation
}

// Hold on to the same instance so that changes to the text made outside are taken into account in `_tseui_updateCharacterCount`.
Expand All @@ -219,7 +224,7 @@ - (void)configureWithTweet:(TWTRSETweet *)tweet
__weak typeof(self) weakSelf = self;
[cocoaItemProviderAttachment afterURLLoadPerform:^{
__strong typeof(self) strongSelf = weakSelf;
[strongSelf _tseui_updateCharacterCount]; // will account for attachment URL
[strongSelf _tseui_updateCharacterCount]; // will account for attachment URL
}];
}
self.attachmentView = [[TWTRSETweetAttachmentView alloc] initWithAttachment:tweet.attachment];
Expand All @@ -245,14 +250,13 @@ - (void)updateText:(NSString *)text

- (void)setTextSelection:(NSRange)textSelection
{
if (! NSEqualRanges(textSelection, _textSelection)) {
if (!NSEqualRanges(textSelection, _textSelection)) {
_textSelection = textSelection;

if (textSelection.location != NSNotFound && NSMaxRange(textSelection) <= self.textView.text.length) {
self.textView.selectedRange = textSelection;
} else {
NSAssert(textSelection.location != NSNotFound && NSMaxRange(textSelection) <= self.textView.text.length,
@"(range %@ exceeds text length %u)", NSStringFromRange(textSelection), (unsigned)self.textView.text.length);
NSAssert(textSelection.location != NSNotFound && NSMaxRange(textSelection) <= self.textView.text.length, @"(range %@ exceeds text length %u)", NSStringFromRange(textSelection), (unsigned)self.textView.text.length);
}
}
}
Expand Down Expand Up @@ -296,16 +300,19 @@ - (void)_scrollToCursorWithRetry:(BOOL)retry
- (void)_tseui_hidePlaceholder:(BOOL)hidden
{
if (![_placeholderLabel isHidden] && hidden) {
[UIView animateWithDuration:0.2 animations:^{
self->_placeholderLabel.alpha = 0;
} completion:^(BOOL finished) {
self->_placeholderLabel.hidden = YES;
}];
[UIView animateWithDuration:0.2
animations:^{
self->_placeholderLabel.alpha = 0;
}
completion:^(BOOL finished) {
self->_placeholderLabel.hidden = YES;
}];
} else if ([_placeholderLabel isHidden] && !hidden) {
_placeholderLabel.hidden = NO;
[UIView animateWithDuration:0.2 animations:^{
self->_placeholderLabel.alpha = 1;
}];
[UIView animateWithDuration:0.2
animations:^{
self->_placeholderLabel.alpha = 1;
}];
}
}

Expand Down

0 comments on commit ba813f0

Please sign in to comment.