diff --git a/Source/Classes/SLKTextInputbar.m b/Source/Classes/SLKTextInputbar.m index 45ecbe74..58d45c4d 100644 --- a/Source/Classes/SLKTextInputbar.m +++ b/Source/Classes/SLKTextInputbar.m @@ -29,6 +29,8 @@ @interface SLKTextInputbar () @property (nonatomic, strong) NSLayoutConstraint *bottomMarginWC; @property (nonatomic, strong) NSLayoutConstraint *rightButtonWC; @property (nonatomic, strong) NSLayoutConstraint *rightMarginWC; +@property (nonatomic, strong) NSLayoutConstraint *rightButtonTopMarginC; +@property (nonatomic, strong) NSLayoutConstraint *rightButtonBottomMarginC; @property (nonatomic, strong) NSLayoutConstraint *editorContentViewHC; @property (nonatomic, strong) NSArray *charCountLabelVCs; @@ -92,6 +94,7 @@ - (void)slk_commonInit [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slk_didChangeTextViewContentSize:) name:SLKTextViewContentSizeDidChangeNotification object:nil]; [self.leftButton.imageView addObserver:self forKeyPath:NSStringFromSelector(@selector(image)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; + [self.rightButton.titleLabel addObserver:self forKeyPath:NSStringFromSelector(@selector(font)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; } @@ -580,11 +583,8 @@ - (void)slk_didChangeTextViewContentSize:(NSNotification *)notification - (void)slk_setupViewConstraints { - UIImage *leftButtonImg = [self.leftButton imageForState:UIControlStateNormal]; - [self.rightButton sizeToFit]; - CGFloat leftVerMargin = (self.intrinsicContentSize.height - leftButtonImg.size.height) / 2.0; CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0; NSDictionary *views = @{@"textView": self.textView, @@ -598,7 +598,6 @@ - (void)slk_setupViewConstraints @"bottom" : @(self.contentInset.bottom), @"left" : @(self.contentInset.left), @"right" : @(self.contentInset.right), - @"leftVerMargin" : @(leftVerMargin), @"rightVerMargin" : @(rightVerMargin), @"minTextViewHeight" : @(self.textView.intrinsicContentSize.height), }; @@ -621,6 +620,9 @@ - (void)slk_setupViewConstraints self.rightButtonWC = [self slk_constraintForAttribute:NSLayoutAttributeWidth firstItem:self.rightButton secondItem:nil]; self.rightMarginWC = [self slk_constraintsForAttribute:NSLayoutAttributeTrailing][0]; + + self.rightButtonTopMarginC = [self slk_constraintForAttribute:NSLayoutAttributeTop firstItem:self.rightButton secondItem:self]; + self.rightButtonBottomMarginC = [self slk_constraintForAttribute:NSLayoutAttributeBottom firstItem:self secondItem:self.rightButton]; } - (void)slk_updateConstraintConstants @@ -652,6 +654,13 @@ - (void)slk_updateConstraintConstants self.rightButtonWC.constant = [self slk_appropriateRightButtonWidth]; self.rightMarginWC.constant = [self slk_appropriateRightButtonMargin]; + + [self.rightButton sizeToFit]; + + CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0; + + self.rightButtonTopMarginC.constant = rightVerMargin; + self.rightButtonBottomMarginC.constant = rightVerMargin; } } @@ -669,6 +678,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N [self slk_updateConstraintConstants]; } + else if ([object isEqual:self.rightButton.titleLabel] && [keyPath isEqualToString:NSStringFromSelector(@selector(font))]) { + [self slk_updateConstraintConstants]; + } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } @@ -683,6 +695,7 @@ - (void)dealloc [[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewContentSizeDidChangeNotification object:nil]; [_leftButton.imageView removeObserver:self forKeyPath:NSStringFromSelector(@selector(image))]; + [_rightButton.titleLabel removeObserver:self forKeyPath:NSStringFromSelector(@selector(font))]; _leftButton = nil; _rightButton = nil; @@ -701,6 +714,8 @@ - (void)dealloc _bottomMarginWC = nil; _rightButtonWC = nil; _rightMarginWC = nil; + _rightButtonTopMarginC = nil; + _rightButtonBottomMarginC = nil; _editorContentViewHC = nil; }