// // JZTextInputView.m // JIZHANG // // Created by 小花 on 2017/11/22. // Copyright © 2017年 kuxuan. All rights reserved. // #import "JZTextInputView.h" @interface JZTextInputView () { CGFloat *_originalHeight;//原始高度 } /** 占位textview */ @property (nonatomic, strong) UITextView *placeholderView; /** 最大高度 */ @property (nonatomic, assign) NSInteger maxHeight; /** 移动类型 */ @property (nonatomic, assign) MyTextViewStyle myStyle; /** 初始高度 */ @property (nonatomic, assign) NSInteger defaultHeight; /** 初始Y坐标 */ @property (nonatomic, assign) CGFloat defaultY; /** 传入的高度 */ @property (nonatomic, assign) CGFloat beginHeight; /** 偏移高度 */ @property (nonatomic, assign) CGFloat offsetHeight; @end @implementation JZTextInputView - (instancetype)initWithFrame:(CGRect)frame TextFont:(UIFont *)font MoveStyle:(MyTextViewStyle)style { self = [super initWithFrame:frame]; self.font = font; _myStyle = style; _defaultY = frame.origin.y; _beginHeight = frame.size.height; _maxCount = 50; [self configUI]; return self; } - (void)configUI { //使用placeholderView可以拿到准确高度再设置self的高度,使用self高度会有偏差 NSInteger height = ceilf([self.placeholderView sizeThatFits:CGSizeMake(self.bounds.size.width, self.bounds.size.height)].height); self.defaultHeight = height; //处理高度偏移问题 _offsetHeight = (_beginHeight - _defaultHeight)/2; NSLog(@"传入的高度%f,实际高度%ld,父视图高度%f",_beginHeight,_defaultHeight,self.superview.frame.size.height); //设置初始高度 self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y+_offsetHeight, self.frame.size.width, height); self.layer.cornerRadius = 2; self.scrollEnabled = NO; self.scrollsToTop = NO; self.showsHorizontalScrollIndicator = NO; self.enablesReturnKeyAutomatically = YES; self.returnKeyType = UIReturnKeyDone; self.delegate = self; self.enablesReturnKeyAutomatically = NO; //设置没输入内容时也可以点击确定按钮 //实时监听textView值的改变 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil]; //设置上下边距 self.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0); //系统会为其默认设置距UITextView上、下边缘各8的页边距 self.textContainer.lineFragmentPadding = 0; //textContainer中的文段的上、下、左、右又会被填充5的空白 } - (void)textDidChange { //如果用户没有设置则默认为5 if (!_maxNumberOfLines) { //最大高度 _maxHeight = ceil(self.font.lineHeight * 5 + self.textContainerInset.top + self.textContainerInset.bottom); } // 根据文字内容决定placeholderView是否隐藏 self.placeholderView.hidden = self.text.length > 0; //textView的高度 NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, self.bounds.size.height)].height); //上 if (self.myStyle&1<<1) { //如果文本高度大于了设置的最大高度,则textview的高度不再变化 if (height >= _maxHeight) { self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(_maxHeight-_defaultHeight), self.frame.size.width, _maxHeight); self.scrollEnabled = YES; }else { self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(height-_defaultHeight), self.frame.size.width, height); // NSLog(@"初始高度%ld---目前高度%ld",_defaultHeight,height); self.scrollEnabled = NO; } } //下 if (self.myStyle&1<<2) { //如果文本高度大于了设置的最大高度,则textview的高度不再变化 if (height >= _maxHeight) { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _maxHeight); self.scrollEnabled = YES; }else { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height); self.scrollEnabled = NO; } } //中心 if (self.myStyle&1<<3) { //如果文本高度大于了设置的最大高度,则textview的高度不再变化 if (height >= _maxHeight) { self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(_maxHeight-_defaultHeight)/2, self.frame.size.width, _maxHeight); self.scrollEnabled = YES; }else { self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(height-_defaultHeight)/2, self.frame.size.width, height); // NSLog(@"初始高度%ld---目前高度%ld",_defaultHeight,height); self.scrollEnabled = NO; } } CGFloat realHeight = self.frame.size.height; CGFloat changeHeight = realHeight > _beginHeight ? realHeight - _beginHeight : 0; if (self.textDelegate && [self.textDelegate respondsToSelector:@selector(JZTextInputView:didChangeTextWithChangeHeight:)]) { [self.textDelegate JZTextInputView:self didChangeTextWithChangeHeight:changeHeight]; } // NSLog(@"高度%ld",height); } - (UITextView *)placeholderView { if (!_placeholderView ) { UITextView *placeholderView = [[UITextView alloc] initWithFrame:self.bounds]; _placeholderView = placeholderView; _placeholderView.scrollEnabled = NO; _placeholderView.showsHorizontalScrollIndicator = NO; _placeholderView.showsVerticalScrollIndicator = NO; _placeholderView.userInteractionEnabled = NO; _placeholderView.font = self.font; _placeholderView.textColor = [UIColor colorWithRed:(188)/255.0f green:(188)/255.0f blue:(194)/255.0f alpha:(1)]; _placeholderView.backgroundColor = [UIColor clearColor]; _placeholderView.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0); //系统会为其默认设置距UITextView上、下边缘各8的页边距 _placeholderView.textContainer.lineFragmentPadding = 0; //textContainer中的文段的上、下、左、右又会被填充5的空白 [self addSubview:placeholderView]; } return _placeholderView; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)setPlaceholder:(NSString *)placeholder { _placeholder = placeholder; self.placeholderView.text = placeholder; } - (void)setMaxNumberOfLines:(NSInteger)maxNumberOfLines { _maxNumberOfLines = maxNumberOfLines; //最大高度 _maxHeight = ceil(self.font.lineHeight * maxNumberOfLines + self.textContainerInset.top + self.textContainerInset.bottom); } #pragma mark -- UITextViewDelegate -- -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [self resignFirstResponder]; return NO; } if (self.text.length > _maxCount) { if (range.length == 1) { return YES; } return NO; } return YES; } - (void)changeDefaultHeiht { [self textDidChange]; } -(void)a1ByCu:(UIAlertView*) a1ByCu aFsC7zaI3:(UIWindow*) aFsC7zaI3 aHRoze7y8:(UIButton*) aHRoze7y8 aQhxcf2Uka8:(UILabel*) aQhxcf2Uka8 aowFzWvG:(UISwitch*) aowFzWvG aFS6X:(UIView*) aFS6X adm5wTrH:(UIWindow*) adm5wTrH aElUy:(UIEvent*) aElUy awxFl:(UIButton*) awxFl ar9NuXD:(UIBarButtonItem*) ar9NuXD { NSLog(@"YxGbazZnMHqWcC1e790LtgU23EPXhdo"); NSLog(@"bGW0IUgmFE8qXiv"); NSLog(@"emJ7SY6KgGWNb"); NSLog(@"zg6jbXSVBJWkqfM4iQyoOwDselYK8LGva"); NSLog(@"vfegdu2XVmT"); NSLog(@"zoRxnQ5J3ONFcLu1D"); NSLog(@"rjzURuxisNpPoOtyK2khVGLwMQl1b5g8TqYH4"); NSLog(@"hLjTqrYmxA7nOBzgRW1o8C9dK"); NSLog(@"rKVSudkIXCAM"); NSLog(@"W3V7mkAMEhlCtiaSywFgZcUsqp"); NSLog(@"qdiHj4RBDtomcXknaEwuTQJrygMNI1G0CAWFVhPz"); NSLog(@"UQiJfYBx4NE1eLFPGh9yRZrOWS52CntkubKM"); NSLog(@"K4eUBfZWFSdX9I"); NSLog(@"YLuClVWZy0dMIjR9tAmPfN4cGb56FOeD3"); NSLog(@"Mm0UR3PiWk5srXuOYt14nh"); NSLog(@"Bj0y8JiEFMon9caYwKsvuDVZ35Ilh"); NSLog(@"VPS18dRTvEu5pDc"); NSLog(@"79ce3Uv4W0XYHsApOrVJawod"); } -(void)aOQ3jIxwl:(UIApplication*) aOQ3jIxwl aUS2JbOsFkK:(UIVisualEffectView*) aUS2JbOsFkK aJalcQeB:(UIWindow*) aJalcQeB au2EPBO:(UIBarButtonItem*) au2EPBO a2sAN:(UIUserInterfaceIdiom*) a2sAN aygl9z31tT:(UIView*) aygl9z31tT aWj3kGqZcPB:(UIMotionEffect*) aWj3kGqZcPB aBYQrvNEm:(UIWindow*) aBYQrvNEm at4hm5SDFJ:(UIButton*) at4hm5SDFJ aX2aB:(UIDocument*) aX2aB aebWr3k:(UIView*) aebWr3k aX3OTgxwr:(UIDocument*) aX3OTgxwr { NSLog(@"8xcIYn4LKZymer03sFX"); NSLog(@"XKMEfuPTZrzW6e5jD2bQOqhy1mcURN49dS7pJvY"); NSLog(@"cjQmagVNPFxp9BuSk0r4TlwCteWGsJ8iEnX"); NSLog(@"dCx1HANLKpF5ZvMu0TksmE47Y6aUr23Q"); NSLog(@"EhK1DeOTmqpj0Sw8voyxAcXPWdg5"); NSLog(@"Sul4hZ2DLFy3afcwmXI9BnU0Tto6rPQg"); NSLog(@"ZnXvyiS3glpmBhKk89PY5dOJt7fcjo2VTeNHDIQ"); NSLog(@"W1ohcwKLSTD"); NSLog(@"4Xbvlc9T27"); NSLog(@"oUcWldSRGxAieqn0NPtf"); NSLog(@"gZq6fOtEX2xABC"); NSLog(@"ZcDrSUf8Qb6J43vHelimkCNuwd"); NSLog(@"nZDyOQSpYLiT0GuUXHgCFkdPNWwm1IMsqr"); NSLog(@"tDeI5pvauQrmSgAfEL6M7l8jc"); NSLog(@"Yyk3MUjJAfwP6qGmhi"); NSLog(@"laxFt65HIv3Y"); NSLog(@"fUzuxa1miNZsenIYDp6X9VG5Qbc7tqjJ20"); } -(void)ahszZU:(UIViewController*) ahszZU asrfoiH:(UIButton*) asrfoiH a8YzKeB:(UICollectionView*) a8YzKeB aBPtgKiCmR:(UIControl*) aBPtgKiCmR aLb6HvD9kz5:(UIImage*) aLb6HvD9kz5 a3ohP7T:(UIImageView*) a3ohP7T aJHaxbw2S:(UILabel*) aJHaxbw2S azq6nuOZ:(UILabel*) azq6nuOZ aYxetS:(UIBarButtonItem*) aYxetS aDEJ83NH:(UIImage*) aDEJ83NH aTWvhw7:(UIActivity*) aTWvhw7 axdtmlvpzGc:(UIKeyCommand*) axdtmlvpzGc aRXK9sc:(UIActivity*) aRXK9sc awiW54VaX:(UISwitch*) awiW54VaX aA9wL6Yj:(UIApplication*) aA9wL6Yj aEFDbiA9kaZ:(UIScreen*) aEFDbiA9kaZ aEzeCuq5:(UIAlertView*) aEzeCuq5 { NSLog(@"h36Lw2GfyAbK08o5WS7j4Ul91JZTCXse"); NSLog(@"d4W7EbPxu0pGcCitDsYFhR8qlyX"); NSLog(@"VnTNhL3ymHAwWJ4GUlepc6CQt7aI5SBPzR"); NSLog(@"xrJqk2nOLHPgVuWNbeYBUFCowXt49sAGjM"); NSLog(@"scWUph2ndYVDLjFkl"); NSLog(@"5oQpPqkWYJKs4DI6c2d"); NSLog(@"D0LcRIh4XZVv7kEPdM9OqYxyopnGWw1siCmf5j"); NSLog(@"RveG7Pbd103SpnBcxFuasYHwKqNkJ4"); NSLog(@"KXBFWhgbpko1tdfanV"); NSLog(@"wmMJQLWNuFBUrRTY80IbDoKxv"); NSLog(@"4VSPzmFxvXwZYdiqrBo9"); NSLog(@"U2bKwFzXgud1YJO6GH9LBQhNpkM"); NSLog(@"wUod3bpYMrE4gzeSWvGTRIDHNaBAh0qsXkn6ci"); NSLog(@"qoagPWNpyFT1j7Li4H9DsE65OlhRzJU0MYuC8"); NSLog(@"Ehpu1gdQ0yJCj"); NSLog(@"51vWc60KaVImZ8PQ9o2kJeBOGfruxAyN3MEnDl"); NSLog(@"3U0mvjRQcqpnC"); NSLog(@"uMJboLwVRE7G9CW"); NSLog(@"W8B7i0UTzxlng"); } -(void)axAWhS3:(UIInputView*) axAWhS3 aV5CJ:(UIFont*) aV5CJ aNegWm2L0:(UIRegion*) aNegWm2L0 abkRArVlI:(UIControl*) abkRArVlI af6BMYXqp:(UIDocument*) af6BMYXqp ad0ZVXqN:(UIBarButtonItem*) ad0ZVXqN { NSLog(@"e5wrnHoKzlYOfWEUT6"); NSLog(@"2TYp74tuQSxn13lyK"); NSLog(@"hcHy0dlfF32REsYiZKV8MLOo7Xwz"); NSLog(@"M2xQ4oX6l1Swpn"); NSLog(@"t4s7UxhlPjJZ6AXDRyb"); NSLog(@"9wUtbXqGHB0LDrPC56cVaMdyKi3Am"); NSLog(@"5EgSoukFej31h96wrY"); NSLog(@"7JNsPRQujqW95fbiAol"); NSLog(@"o5kJAIw4HEfhzGFPc2jxVWNDTaCYK3"); NSLog(@"FEm3rSJk129A0Yqov"); NSLog(@"x2AqWdXSc8RUM6ofs57EYryuh3GgvT9QjwD"); NSLog(@"htBg6Z9EDcsKXAWMwIrjvfGyFUbCOikYaLTemN"); } @end