Skip to content

Commit d2ca89d

Browse files
author
Ignacio Romero Zurbuchen
committed
Merge pull request slackhq#186 from jmreyes/rightbutton-resize-center
Center right button and update constraints when its size is changed
2 parents f1ba997 + 27d1515 commit d2ca89d

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Source/Classes/SLKTextInputbar.m

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ @interface SLKTextInputbar ()
2929
@property (nonatomic, strong) NSLayoutConstraint *bottomMarginWC;
3030
@property (nonatomic, strong) NSLayoutConstraint *rightButtonWC;
3131
@property (nonatomic, strong) NSLayoutConstraint *rightMarginWC;
32+
@property (nonatomic, strong) NSLayoutConstraint *rightButtonTopMarginC;
33+
@property (nonatomic, strong) NSLayoutConstraint *rightButtonBottomMarginC;
3234
@property (nonatomic, strong) NSLayoutConstraint *editorContentViewHC;
3335
@property (nonatomic, strong) NSArray *charCountLabelVCs;
3436

@@ -92,6 +94,7 @@ - (void)slk_commonInit
9294
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slk_didChangeTextViewContentSize:) name:SLKTextViewContentSizeDidChangeNotification object:nil];
9395

9496
[self.leftButton.imageView addObserver:self forKeyPath:NSStringFromSelector(@selector(image)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
97+
[self.rightButton.titleLabel addObserver:self forKeyPath:NSStringFromSelector(@selector(font)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
9598
}
9699

97100

@@ -580,11 +583,8 @@ - (void)slk_didChangeTextViewContentSize:(NSNotification *)notification
580583

581584
- (void)slk_setupViewConstraints
582585
{
583-
UIImage *leftButtonImg = [self.leftButton imageForState:UIControlStateNormal];
584-
585586
[self.rightButton sizeToFit];
586587

587-
CGFloat leftVerMargin = (self.intrinsicContentSize.height - leftButtonImg.size.height) / 2.0;
588588
CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0;
589589

590590
NSDictionary *views = @{@"textView": self.textView,
@@ -598,7 +598,6 @@ - (void)slk_setupViewConstraints
598598
@"bottom" : @(self.contentInset.bottom),
599599
@"left" : @(self.contentInset.left),
600600
@"right" : @(self.contentInset.right),
601-
@"leftVerMargin" : @(leftVerMargin),
602601
@"rightVerMargin" : @(rightVerMargin),
603602
@"minTextViewHeight" : @(self.textView.intrinsicContentSize.height),
604603
};
@@ -621,6 +620,9 @@ - (void)slk_setupViewConstraints
621620

622621
self.rightButtonWC = [self slk_constraintForAttribute:NSLayoutAttributeWidth firstItem:self.rightButton secondItem:nil];
623622
self.rightMarginWC = [self slk_constraintsForAttribute:NSLayoutAttributeTrailing][0];
623+
624+
self.rightButtonTopMarginC = [self slk_constraintForAttribute:NSLayoutAttributeTop firstItem:self.rightButton secondItem:self];
625+
self.rightButtonBottomMarginC = [self slk_constraintForAttribute:NSLayoutAttributeBottom firstItem:self secondItem:self.rightButton];
624626
}
625627

626628
- (void)slk_updateConstraintConstants
@@ -652,6 +654,13 @@ - (void)slk_updateConstraintConstants
652654

653655
self.rightButtonWC.constant = [self slk_appropriateRightButtonWidth];
654656
self.rightMarginWC.constant = [self slk_appropriateRightButtonMargin];
657+
658+
[self.rightButton sizeToFit];
659+
660+
CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0;
661+
662+
self.rightButtonTopMarginC.constant = rightVerMargin;
663+
self.rightButtonBottomMarginC.constant = rightVerMargin;
655664
}
656665
}
657666

@@ -669,6 +678,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
669678

670679
[self slk_updateConstraintConstants];
671680
}
681+
else if ([object isEqual:self.rightButton.titleLabel] && [keyPath isEqualToString:NSStringFromSelector(@selector(font))]) {
682+
[self slk_updateConstraintConstants];
683+
}
672684
else {
673685
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
674686
}
@@ -683,6 +695,7 @@ - (void)dealloc
683695
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewContentSizeDidChangeNotification object:nil];
684696

685697
[_leftButton.imageView removeObserver:self forKeyPath:NSStringFromSelector(@selector(image))];
698+
[_rightButton.titleLabel removeObserver:self forKeyPath:NSStringFromSelector(@selector(font))];
686699

687700
_leftButton = nil;
688701
_rightButton = nil;
@@ -701,6 +714,8 @@ - (void)dealloc
701714
_bottomMarginWC = nil;
702715
_rightButtonWC = nil;
703716
_rightMarginWC = nil;
717+
_rightButtonTopMarginC = nil;
718+
_rightButtonBottomMarginC = nil;
704719
_editorContentViewHC = nil;
705720
}
706721

0 commit comments

Comments
 (0)