Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #186 from jmreyes/rightbutton-resize-center
Browse files Browse the repository at this point in the history
Center right button and update constraints when its size is changed
  • Loading branch information
Ignacio Romero Zurbuchen committed Aug 7, 2015
2 parents f1ba997 + 27d1515 commit d2ca89d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Source/Classes/SLKTextInputbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
}


Expand Down Expand Up @@ -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,
Expand All @@ -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),
};
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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];
}
Expand All @@ -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;
Expand All @@ -701,6 +714,8 @@ - (void)dealloc
_bottomMarginWC = nil;
_rightButtonWC = nil;
_rightMarginWC = nil;
_rightButtonTopMarginC = nil;
_rightButtonBottomMarginC = nil;
_editorContentViewHC = nil;
}

Expand Down

0 comments on commit d2ca89d

Please sign in to comment.