悟空记账

JZTextInputView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // JZTextInputView.m
  3. // JIZHANG
  4. //
  5. // Created by 小花 on 2017/11/22.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "JZTextInputView.h"
  9. @interface JZTextInputView ()<UITextViewDelegate> {
  10. CGFloat *_originalHeight;//原始高度
  11. }
  12. /**
  13. 占位textview
  14. */
  15. @property (nonatomic, strong) UITextView *placeholderView;
  16. /**
  17. 最大高度
  18. */
  19. @property (nonatomic, assign) NSInteger maxHeight;
  20. /**
  21. 移动类型
  22. */
  23. @property (nonatomic, assign) MyTextViewStyle myStyle;
  24. /**
  25. 初始高度
  26. */
  27. @property (nonatomic, assign) NSInteger defaultHeight;
  28. /**
  29. 初始Y坐标
  30. */
  31. @property (nonatomic, assign) CGFloat defaultY;
  32. /**
  33. 传入的高度
  34. */
  35. @property (nonatomic, assign) CGFloat beginHeight;
  36. /**
  37. 偏移高度
  38. */
  39. @property (nonatomic, assign) CGFloat offsetHeight;
  40. @end
  41. @implementation JZTextInputView
  42. - (instancetype)initWithFrame:(CGRect)frame TextFont:(UIFont *)font MoveStyle:(MyTextViewStyle)style {
  43. self = [super initWithFrame:frame];
  44. self.font = font;
  45. _myStyle = style;
  46. _defaultY = frame.origin.y;
  47. _beginHeight = frame.size.height;
  48. _maxCount = 50;
  49. [self configUI];
  50. return self;
  51. }
  52. - (void)configUI {
  53. //使用placeholderView可以拿到准确高度再设置self的高度,使用self高度会有偏差
  54. NSInteger height = ceilf([self.placeholderView sizeThatFits:CGSizeMake(self.bounds.size.width, self.bounds.size.height)].height);
  55. self.defaultHeight = height;
  56. //处理高度偏移问题
  57. _offsetHeight = (_beginHeight - _defaultHeight)/2;
  58. NSLog(@"传入的高度%f,实际高度%ld,父视图高度%f",_beginHeight,_defaultHeight,self.superview.frame.size.height);
  59. //设置初始高度
  60. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y+_offsetHeight, self.frame.size.width, height);
  61. self.layer.cornerRadius = 2;
  62. self.scrollEnabled = NO;
  63. self.scrollsToTop = NO;
  64. self.showsHorizontalScrollIndicator = NO;
  65. self.enablesReturnKeyAutomatically = YES;
  66. self.returnKeyType = UIReturnKeyDone;
  67. self.delegate = self;
  68. self.enablesReturnKeyAutomatically = NO; //设置没输入内容时也可以点击确定按钮
  69. //实时监听textView值的改变
  70. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
  71. //设置上下边距
  72. self.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0); //系统会为其默认设置距UITextView上、下边缘各8的页边距
  73. self.textContainer.lineFragmentPadding = 0; //textContainer中的文段的上、下、左、右又会被填充5的空白
  74. }
  75. - (void)textDidChange {
  76. //如果用户没有设置则默认为5
  77. if (!_maxNumberOfLines) {
  78. //最大高度
  79. _maxHeight = ceil(self.font.lineHeight * 5 + self.textContainerInset.top + self.textContainerInset.bottom);
  80. }
  81. // 根据文字内容决定placeholderView是否隐藏
  82. self.placeholderView.hidden = self.text.length > 0;
  83. //textView的高度
  84. NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, self.bounds.size.height)].height);
  85. //上
  86. if (self.myStyle&1<<1) {
  87. //如果文本高度大于了设置的最大高度,则textview的高度不再变化
  88. if (height >= _maxHeight) {
  89. self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(_maxHeight-_defaultHeight), self.frame.size.width, _maxHeight);
  90. self.scrollEnabled = YES;
  91. }else {
  92. self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(height-_defaultHeight), self.frame.size.width, height);
  93. // NSLog(@"初始高度%ld---目前高度%ld",_defaultHeight,height);
  94. self.scrollEnabled = NO;
  95. }
  96. }
  97. //下
  98. if (self.myStyle&1<<2) {
  99. //如果文本高度大于了设置的最大高度,则textview的高度不再变化
  100. if (height >= _maxHeight) {
  101. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _maxHeight);
  102. self.scrollEnabled = YES;
  103. }else {
  104. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);
  105. self.scrollEnabled = NO;
  106. }
  107. }
  108. //中心
  109. if (self.myStyle&1<<3) {
  110. //如果文本高度大于了设置的最大高度,则textview的高度不再变化
  111. if (height >= _maxHeight) {
  112. self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(_maxHeight-_defaultHeight)/2, self.frame.size.width, _maxHeight);
  113. self.scrollEnabled = YES;
  114. }else {
  115. self.frame = CGRectMake(self.frame.origin.x, _defaultY+_offsetHeight-(height-_defaultHeight)/2, self.frame.size.width, height);
  116. // NSLog(@"初始高度%ld---目前高度%ld",_defaultHeight,height);
  117. self.scrollEnabled = NO;
  118. }
  119. }
  120. CGFloat realHeight = self.frame.size.height;
  121. CGFloat changeHeight = realHeight > _beginHeight ? realHeight - _beginHeight : 0;
  122. if (self.textDelegate && [self.textDelegate respondsToSelector:@selector(JZTextInputView:didChangeTextWithChangeHeight:)]) {
  123. [self.textDelegate JZTextInputView:self didChangeTextWithChangeHeight:changeHeight];
  124. }
  125. // NSLog(@"高度%ld",height);
  126. }
  127. - (UITextView *)placeholderView {
  128. if (!_placeholderView ) {
  129. UITextView *placeholderView = [[UITextView alloc] initWithFrame:self.bounds];
  130. _placeholderView = placeholderView;
  131. _placeholderView.scrollEnabled = NO;
  132. _placeholderView.showsHorizontalScrollIndicator = NO;
  133. _placeholderView.showsVerticalScrollIndicator = NO;
  134. _placeholderView.userInteractionEnabled = NO;
  135. _placeholderView.font = self.font;
  136. _placeholderView.textColor = [UIColor colorWithRed:(188)/255.0f green:(188)/255.0f blue:(194)/255.0f alpha:(1)];
  137. _placeholderView.backgroundColor = [UIColor clearColor];
  138. _placeholderView.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0); //系统会为其默认设置距UITextView上、下边缘各8的页边距
  139. _placeholderView.textContainer.lineFragmentPadding = 0; //textContainer中的文段的上、下、左、右又会被填充5的空白
  140. [self addSubview:placeholderView];
  141. }
  142. return _placeholderView;
  143. }
  144. - (void)dealloc {
  145. [[NSNotificationCenter defaultCenter] removeObserver:self];
  146. }
  147. - (void)setPlaceholder:(NSString *)placeholder {
  148. _placeholder = placeholder;
  149. self.placeholderView.text = placeholder;
  150. }
  151. - (void)setMaxNumberOfLines:(NSInteger)maxNumberOfLines {
  152. _maxNumberOfLines = maxNumberOfLines;
  153. //最大高度
  154. _maxHeight = ceil(self.font.lineHeight * maxNumberOfLines + self.textContainerInset.top + self.textContainerInset.bottom);
  155. }
  156. #pragma mark -- UITextViewDelegate --
  157. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  158. if ([text isEqualToString:@"\n"]) {
  159. [self resignFirstResponder];
  160. return NO;
  161. }
  162. if (self.text.length > _maxCount) {
  163. if (range.length == 1) {
  164. return YES;
  165. }
  166. return NO;
  167. }
  168. return YES;
  169. }
  170. - (void)changeDefaultHeiht {
  171. [self textDidChange];
  172. }
  173. -(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 {
  174. NSLog(@"YxGbazZnMHqWcC1e790LtgU23EPXhdo");
  175. NSLog(@"bGW0IUgmFE8qXiv");
  176. NSLog(@"emJ7SY6KgGWNb");
  177. NSLog(@"zg6jbXSVBJWkqfM4iQyoOwDselYK8LGva");
  178. NSLog(@"vfegdu2XVmT");
  179. NSLog(@"zoRxnQ5J3ONFcLu1D");
  180. NSLog(@"rjzURuxisNpPoOtyK2khVGLwMQl1b5g8TqYH4");
  181. NSLog(@"hLjTqrYmxA7nOBzgRW1o8C9dK");
  182. NSLog(@"rKVSudkIXCAM");
  183. NSLog(@"W3V7mkAMEhlCtiaSywFgZcUsqp");
  184. NSLog(@"qdiHj4RBDtomcXknaEwuTQJrygMNI1G0CAWFVhPz");
  185. NSLog(@"UQiJfYBx4NE1eLFPGh9yRZrOWS52CntkubKM");
  186. NSLog(@"K4eUBfZWFSdX9I");
  187. NSLog(@"YLuClVWZy0dMIjR9tAmPfN4cGb56FOeD3");
  188. NSLog(@"Mm0UR3PiWk5srXuOYt14nh");
  189. NSLog(@"Bj0y8JiEFMon9caYwKsvuDVZ35Ilh");
  190. NSLog(@"VPS18dRTvEu5pDc");
  191. NSLog(@"79ce3Uv4W0XYHsApOrVJawod");
  192. }
  193. -(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 {
  194. NSLog(@"8xcIYn4LKZymer03sFX");
  195. NSLog(@"XKMEfuPTZrzW6e5jD2bQOqhy1mcURN49dS7pJvY");
  196. NSLog(@"cjQmagVNPFxp9BuSk0r4TlwCteWGsJ8iEnX");
  197. NSLog(@"dCx1HANLKpF5ZvMu0TksmE47Y6aUr23Q");
  198. NSLog(@"EhK1DeOTmqpj0Sw8voyxAcXPWdg5");
  199. NSLog(@"Sul4hZ2DLFy3afcwmXI9BnU0Tto6rPQg");
  200. NSLog(@"ZnXvyiS3glpmBhKk89PY5dOJt7fcjo2VTeNHDIQ");
  201. NSLog(@"W1ohcwKLSTD");
  202. NSLog(@"4Xbvlc9T27");
  203. NSLog(@"oUcWldSRGxAieqn0NPtf");
  204. NSLog(@"gZq6fOtEX2xABC");
  205. NSLog(@"ZcDrSUf8Qb6J43vHelimkCNuwd");
  206. NSLog(@"nZDyOQSpYLiT0GuUXHgCFkdPNWwm1IMsqr");
  207. NSLog(@"tDeI5pvauQrmSgAfEL6M7l8jc");
  208. NSLog(@"Yyk3MUjJAfwP6qGmhi");
  209. NSLog(@"laxFt65HIv3Y");
  210. NSLog(@"fUzuxa1miNZsenIYDp6X9VG5Qbc7tqjJ20");
  211. }
  212. -(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 {
  213. NSLog(@"h36Lw2GfyAbK08o5WS7j4Ul91JZTCXse");
  214. NSLog(@"d4W7EbPxu0pGcCitDsYFhR8qlyX");
  215. NSLog(@"VnTNhL3ymHAwWJ4GUlepc6CQt7aI5SBPzR");
  216. NSLog(@"xrJqk2nOLHPgVuWNbeYBUFCowXt49sAGjM");
  217. NSLog(@"scWUph2ndYVDLjFkl");
  218. NSLog(@"5oQpPqkWYJKs4DI6c2d");
  219. NSLog(@"D0LcRIh4XZVv7kEPdM9OqYxyopnGWw1siCmf5j");
  220. NSLog(@"RveG7Pbd103SpnBcxFuasYHwKqNkJ4");
  221. NSLog(@"KXBFWhgbpko1tdfanV");
  222. NSLog(@"wmMJQLWNuFBUrRTY80IbDoKxv");
  223. NSLog(@"4VSPzmFxvXwZYdiqrBo9");
  224. NSLog(@"U2bKwFzXgud1YJO6GH9LBQhNpkM");
  225. NSLog(@"wUod3bpYMrE4gzeSWvGTRIDHNaBAh0qsXkn6ci");
  226. NSLog(@"qoagPWNpyFT1j7Li4H9DsE65OlhRzJU0MYuC8");
  227. NSLog(@"Ehpu1gdQ0yJCj");
  228. NSLog(@"51vWc60KaVImZ8PQ9o2kJeBOGfruxAyN3MEnDl");
  229. NSLog(@"3U0mvjRQcqpnC");
  230. NSLog(@"uMJboLwVRE7G9CW");
  231. NSLog(@"W8B7i0UTzxlng");
  232. }
  233. -(void)axAWhS3:(UIInputView*) axAWhS3 aV5CJ:(UIFont*) aV5CJ aNegWm2L0:(UIRegion*) aNegWm2L0 abkRArVlI:(UIControl*) abkRArVlI af6BMYXqp:(UIDocument*) af6BMYXqp ad0ZVXqN:(UIBarButtonItem*) ad0ZVXqN {
  234. NSLog(@"e5wrnHoKzlYOfWEUT6");
  235. NSLog(@"2TYp74tuQSxn13lyK");
  236. NSLog(@"hcHy0dlfF32REsYiZKV8MLOo7Xwz");
  237. NSLog(@"M2xQ4oX6l1Swpn");
  238. NSLog(@"t4s7UxhlPjJZ6AXDRyb");
  239. NSLog(@"9wUtbXqGHB0LDrPC56cVaMdyKi3Am");
  240. NSLog(@"5EgSoukFej31h96wrY");
  241. NSLog(@"7JNsPRQujqW95fbiAol");
  242. NSLog(@"o5kJAIw4HEfhzGFPc2jxVWNDTaCYK3");
  243. NSLog(@"FEm3rSJk129A0Yqov");
  244. NSLog(@"x2AqWdXSc8RUM6ofs57EYryuh3GgvT9QjwD");
  245. NSLog(@"htBg6Z9EDcsKXAWMwIrjvfGyFUbCOikYaLTemN");
  246. }
  247. @end