This commit is contained in:
Pfeil, Scott Robert 2019-02-14 13:01:21 -05:00
commit 83aa4d91e6
2 changed files with 89 additions and 21 deletions

View File

@ -7,6 +7,7 @@
//
#import <UIKit/UIKit.h>
#import "SeparatorView.h"
@interface CustomTextView: UITextView
@ -23,9 +24,16 @@
@interface MFTextView : UIView <UITextViewDelegate>
@property (weak, nonatomic) id delegate;
@property (weak, nonatomic, nullable) id delegate;
@property (weak, nonatomic) IBOutlet CustomTextView *textView;
@property (assign, nonatomic) BOOL hideBorder;
@property (strong, nonatomic, nullable) UIBezierPath *borderPath;
@property (nonatomic, readwrite) BOOL errorShowing;
@property (weak, nonatomic, nullable) SeparatorView *bottomLine;
+(MFTextView *) MFTextViewWithPlaceholderString:(NSString *) placeholder delegate:(id) delegate;
+(MFTextView *_Nullable) MFTextViewWithPlaceholderString:(NSString *_Nullable) placeholder delegate:(id _Nullable ) delegate;
//This method will make the UI as per brand refresh (Similar to UITextField)
- (void)makeBordersForTextView;
@end

View File

@ -10,6 +10,9 @@
#import "UIColor+MFConvenience.h"
#import "MVMCoreUIUtility.h"
#import "MVMCoreUIConstants.h"
#import "MFStyler.h"
#import "MVMCoreUICommonViewsUtility.h"
@import MVMCore.MVMCoreConstants;
@protocol CustomTextViewDelegate <UITextViewDelegate>
@ -18,6 +21,7 @@
-(void) didSetFont:(UIFont *) font;
-(void) didSetContainerInset:(UIEdgeInsets)textContainerInset;
-(void) didSetTextColor:(UIColor *)textColor;
-(void) didSetText:(NSString *)text;
@end
@ -48,7 +52,10 @@
[self.delegateForUIChange didSetTextColor:textColor];
}
- (void)setText:(NSString *)text {
[super setText:text];
[self.delegateForUIChange didSetText:text];
}
@end
@ -72,62 +79,73 @@
-(void)didSetContainerInset:(UIEdgeInsets)textContainerInset {
UILabel *placeHolderLabel = self.placeHolderLabel;
[placeHolderLabel removeConstraints:[self.placeHolderLabel constraints]];
CGSize labelSize = [placeHolderLabel.text sizeWithAttributes:@{NSFontAttributeName: self.placeHolderLabel.font}];
NSArray *constrainsApplied = @[@"PH_TRAILING", @"PH_LEADING", @"PH_TOP"];
for (NSLayoutConstraint *constraint in self.constraints) {
if ([constrainsApplied containsObject:constraint.identifier]) {
[self removeConstraint:constraint];
}
}
[placeHolderLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[placeHolderLabel(height)]" options:0 metrics:@{@"height":@(ceilf(labelSize.height))} views:NSDictionaryOfVariableBindings(placeHolderLabel)]];
[placeHolderLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[placeHolderLabel(width)]" options:0 metrics:@{@"width":@(ceilf(labelSize.width))} views:NSDictionaryOfVariableBindings(placeHolderLabel)]];
NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:placeHolderLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:(textContainerInset.left + 5)];
leadingConstraint.identifier = @"PH_LEADING";
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:placeHolderLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:textContainerInset.top];
topConstraint.identifier = @"PH_TOP";
[NSLayoutConstraint activateConstraints:@[leadingConstraint, topConstraint]];
[self updateConstraints];
}
-(void)didSetTextColor:(UIColor *)textColor {
// self.placeHolderLabel.textColor = textColor;
}
- (void)didSetText:(NSString *)text {
[self showOrHidePlaceholderForTextView:self.textView];
}
#pragma Mark UITextView Delegate methods forwarding
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return [self.delegate textViewShouldBeginEditing:textView];
if ([self.delegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
return [self.delegate textViewShouldBeginEditing:textView];
}
return true;
}
-(void)textViewDidBeginEditing:(UITextView *)textView {
[self.delegate textViewDidBeginEditing:textView];
[self showOrHidePlaceholderForTextView:textView];
if ([self.delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) {
[self.delegate textViewDidBeginEditing:textView];
}
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return [self.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
if ([self.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
return [self.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
}
return true;
}
-(void)textViewDidChange:(UITextView *)textView {
[self.delegate textViewDidBeginEditing:textView];
[self showOrHidePlaceholderForTextView:textView];
if ([self.delegate respondsToSelector:@selector(textViewDidChange:)]) {
[self.delegate textViewDidChange:textView];
}
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
return [self.delegate textViewShouldEndEditing:textView];
if ([self.delegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
return [self.delegate textViewShouldEndEditing:textView];
}
return true;
}
-(void)textViewDidEndEditing:(UITextView *)textView {
[self.delegate textViewDidEndEditing:textView];
[self showOrHidePlaceholderForTextView:textView];
if ([self.delegate respondsToSelector:@selector(textViewDidEndEditing:)]) {
[self.delegate textViewDidEndEditing:textView];
}
}
- (void) showOrHidePlaceholderForTextView:(UITextView *) textView {
@ -148,14 +166,12 @@
view.clipsToBounds = YES;
view.translatesAutoresizingMaskIntoConstraints = NO;
view.layer.cornerRadius = CornerRadiusLarge;
view.textView.delegateForUIChange = view;
view.textView.translatesAutoresizingMaskIntoConstraints = NO;
view.placeHolderLabel.text = placeholder;
view.placeHolderLabel.translatesAutoresizingMaskIntoConstraints = NO;
view.placeHolderLabel.textColor = [UIColor mfLightGrayColor];
// Disable SmartQuotes
if (@available(iOS 11.0, *)) {
view.textView.smartQuotesType = UITextSmartQuotesTypeNo;
@ -163,7 +179,51 @@
view.textView.smartInsertDeleteType = UITextSmartInsertDeleteTypeNo;
}
[view didSetFont:view.textView.font];
view.hideBorder = YES;
return view;
}
#pragma mark - bordered textview
- (void)makeBordersForTextView {
SeparatorView *bottomLine = [SeparatorView separatorAddToView:self position:SeparatorPositionBot];
self.bottomLine = bottomLine;
self.placeHolderLabel.font = [MFStyler fontB3];
[self.textView setFont:[MFStyler fontB2]];
[self.textView setTextContainerInset:UIEdgeInsetsMake(PaddingTwo, PaddingTwo, PaddingTwo, PaddingOne)];
[MVMCoreUICommonViewsUtility addDismissToolbarToTextView:self.textView delegate:self.delegate];
self.errorShowing = NO;
self.hideBorder = NO;
[self layoutIfNeeded];
}
- (void)addBezierPathForBorders {
CGRect frame = self.frame;
self.borderPath = [UIBezierPath bezierPath];
[self.borderPath moveToPoint:CGPointMake(frame.origin.x, frame.origin.y + frame.size.height)];
[self.borderPath addLineToPoint:CGPointMake(frame.origin.x, frame.origin.y)];
[self.borderPath addLineToPoint:CGPointMake(frame.origin.x + frame.size.width, frame.origin.y)];
[self.borderPath addLineToPoint:CGPointMake(frame.origin.x + frame.size.width, frame.origin.y + frame.size.height)];
self.borderPath.lineWidth = 1;
UIColor *strokeColor;
if (self.errorShowing) {
strokeColor = [UIColor mfPumpkinColor];
self.bottomLine.backgroundColor = [UIColor mfPumpkinColor];
} else {
strokeColor = [UIColor mfSilver];
self.bottomLine.backgroundColor = [UIColor blackColor];
}
[strokeColor setStroke];
[self.borderPath stroke];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self.borderPath removeAllPoints];
if (!self.hideBorder) {
self.layer.cornerRadius = 0;
[self addBezierPathForBorders];
}
}
@end