ChangeNameViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // ChangeNameViewController.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/7/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "ChangeNameViewController.h"
  9. @interface ChangeNameViewController ()<UITextFieldDelegate>
  10. {
  11. UITextField *_textFiled;
  12. UILabel *_numLabel;
  13. UIButton *_rightButton;
  14. UILabel *_nickInfoLabel;
  15. }
  16. @end
  17. @implementation ChangeNameViewController
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. }
  22. - (void)viewWillAppear:(BOOL)animated
  23. {
  24. [super viewWillAppear:animated];
  25. // [IQKeyboardManager sharedManager].enable = YES;
  26. // [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  27. // [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  28. // [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 5.0f;
  29. self.navigationController.navigationBar.hidden = NO;
  30. }
  31. - (void)viewWillDisappear:(BOOL)animated
  32. {
  33. [super viewWillDisappear:animated];
  34. // [IQKeyboardManager sharedManager].enable = NO;
  35. // [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  36. }
  37. - (void)initFWUI
  38. {
  39. [super initFWUI];
  40. self.view.backgroundColor = kBackGroundColor;
  41. if ([self.viewType isEqualToString:@"1"])
  42. {
  43. self.title = ASLocalizedString(@"编辑昵称");
  44. }else if ([self.viewType isEqualToString:@"2"])
  45. {
  46. self.title = ASLocalizedString(@"编辑签名");
  47. }else if ([self.viewType isEqualToString:@"3"])
  48. {
  49. self.title = ASLocalizedString(@"编辑职业");
  50. }
  51. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(backClick) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  52. _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW-40, 5, 40, 30)];
  53. [_rightButton setTitle:ASLocalizedString(@"保存")forState:UIControlStateNormal];
  54. [_rightButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  55. [_rightButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  56. _rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
  57. [_rightButton addTarget:self action:@selector(saveEditButton) forControlEvents:UIControlEventTouchUpInside];
  58. UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
  59. self.navigationItem.rightBarButtonItem = rightBarButtonItem;
  60. UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kScreenW, 40)];
  61. whiteView.backgroundColor = kWhiteColor;
  62. [self.view addSubview:whiteView];
  63. _textFiled = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenW-20, 40)];
  64. _textFiled.clearsContextBeforeDrawing = YES;
  65. _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
  66. _textFiled.delegate = self;
  67. [_textFiled becomeFirstResponder];
  68. _textFiled.textColor = kAppGrayColor2;
  69. _textFiled.font = [UIFont systemFontOfSize:15];
  70. _textFiled.layer.cornerRadius = 3;
  71. [_textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  72. _textFiled.backgroundColor = [UIColor whiteColor];
  73. [whiteView addSubview:_textFiled];
  74. _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW-80, 60, 60, 20)];
  75. _numLabel.textAlignment = NSTextAlignmentRight;
  76. _numLabel.textColor = kAppGrayColor3;
  77. _numLabel.font = [UIFont systemFontOfSize:15];
  78. [self.view addSubview:_numLabel];
  79. if ([self.viewType isEqualToString:@"2"])
  80. {
  81. _textFiled.placeholder = ASLocalizedString(@"");
  82. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiledName.length];
  83. }else
  84. {
  85. _textFiled.placeholder = ASLocalizedString(@"");
  86. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiledName.length];
  87. }
  88. if (self.textFiledName.length > 0)
  89. {
  90. _textFiled.text = self.textFiledName;
  91. }
  92. if ([self.viewType isEqualToString:@"1"] && self.nickInfo.length>0)
  93. {
  94. _nickInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 90, kScreenW-40, 20)];
  95. _nickInfoLabel.backgroundColor = kBackGroundColor;
  96. _nickInfoLabel.textAlignment = NSTextAlignmentRight;
  97. _nickInfoLabel.textColor = kAppGrayColor3;
  98. _nickInfoLabel.font = kAppSmallTextFont_1;
  99. _nickInfoLabel.text = self.nickInfo;
  100. [self.view addSubview:_nickInfoLabel];
  101. }
  102. }
  103. //限制字符
  104. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  105. {
  106. NSString *comcatstr = [textField.text stringByReplacingCharactersInRange:range withString:string];
  107. NSInteger caninputlen;
  108. if ([self.viewType isEqualToString:@"2"])
  109. {
  110. caninputlen = 16 - comcatstr.length;
  111. }else
  112. {
  113. caninputlen = 16 - comcatstr.length;
  114. }
  115. if (caninputlen >= 0)
  116. {
  117. return YES;
  118. }
  119. else
  120. {
  121. NSInteger len = string.length + caninputlen;
  122. //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错
  123. NSRange rg = {0,MAX(len,0)};
  124. if (rg.length > 0)
  125. {
  126. NSString *s = [string substringWithRange:rg];
  127. [textField setText:[textField.text stringByReplacingCharactersInRange:range withString:s]];
  128. if ([self.viewType isEqualToString:@"2"])
  129. {
  130. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)textField.text.length];
  131. }else
  132. {
  133. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)textField.text.length];
  134. }
  135. }
  136. return NO;
  137. }
  138. }
  139. // 监听改变按钮
  140. - (void)textFieldDidChange:(UITextField*) sender
  141. {
  142. if ([self.viewType isEqualToString:@"2"])
  143. {
  144. if (_textFiled.text.length > 16)
  145. {
  146. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入小于17个文字")];
  147. NSString *s = [_textFiled.text substringToIndex:17];
  148. _textFiled.text = s;
  149. return;
  150. }
  151. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiled.text.length];
  152. }else
  153. {
  154. if (_textFiled.text.length > 16)
  155. {
  156. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入小于17个文字")];
  157. NSString *s = [_textFiled.text substringToIndex:17];
  158. _textFiled.text = s;
  159. return;
  160. }
  161. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiled.text.length];
  162. }
  163. }
  164. - (void)backClick
  165. {
  166. [_textFiled resignFirstResponder];
  167. [self.navigationController popViewControllerAnimated:YES ];
  168. }
  169. //保存
  170. - (void)saveEditButton
  171. {
  172. [_textFiled resignFirstResponder];
  173. NSString *temp = [_textFiled.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  174. if (temp.length < 1)
  175. {
  176. if ([self.viewType isEqualToString:@"1"])
  177. {
  178. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请编辑昵称")];
  179. }else if ([self.viewType isEqualToString:@"2"])
  180. {
  181. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请编辑签名")];
  182. }else if ([self.viewType isEqualToString:@"3"])
  183. {
  184. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请编辑职业")];
  185. }
  186. return;
  187. }
  188. if (self.delegate)
  189. {
  190. if ([self.delegate respondsToSelector:@selector(changeNameWithString:withType:)])
  191. {
  192. [self.delegate changeNameWithString:temp withType:self.viewType];
  193. }
  194. }
  195. [self.navigationController popViewControllerAnimated:YES];
  196. }
  197. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  198. {
  199. [textField resignFirstResponder];
  200. return YES;
  201. }
  202. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  203. {
  204. [_textFiled resignFirstResponder];
  205. }
  206. - (void)textFieldDidEndEditing:(UITextField *)textField
  207. {
  208. if ([self.viewType isEqualToString:@"2"])
  209. {
  210. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiled.text.length];
  211. }else
  212. {
  213. _numLabel.text = [NSString stringWithFormat:@"%d/16",(int)_textFiled.text.length];
  214. }
  215. }
  216. @end