PostSkillsController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // PostSkillsController.m
  3. // model
  4. //
  5. // Created by JuYi on 2018/7/17.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. // 发布技能
  8. #import "PostSkillsController.h"
  9. @interface PostSkillsController ()<UITextFieldDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate>
  10. {
  11. NSString *skillPk;
  12. }
  13. @property (nonatomic, strong) UIScrollView *bgScrollView;
  14. @property (nonatomic, strong) UITextField *workTypeTF; //工作类型
  15. @property (nonatomic, strong) UIImageView *workPicView; //工作照片
  16. @property (nonatomic, strong) UITextField *priceTF; //价格
  17. @property (nonatomic, strong) UIButton *selectedWorkTypeBtn; //选中支付方式按钮
  18. //
  19. @property (nonatomic, copy) NSString *imageStr; //图片url
  20. @property (nonatomic, strong) UIView *bgSkillView;//技能标签背景
  21. @property (strong, nonatomic) NSMutableArray *skillArray;
  22. @end
  23. @implementation PostSkillsController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. self.view.backgroundColor = [UIColor whiteColor];
  28. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
  29. //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
  30. tapGestureRecognizer.cancelsTouchesInView = NO;
  31. //将触摸事件添加到当前view
  32. [self.view addGestureRecognizer:tapGestureRecognizer];
  33. //设置导航条
  34. [self creatNavBar];
  35. //设置子试图
  36. [self getSkillFromNetworking];
  37. }
  38. #pragma mark -- 收起键盘
  39. -(void)keyboardHide:(UITapGestureRecognizer*)tap{
  40. [self.view endEditing:YES];
  41. }
  42. - (void)didReceiveMemoryWarning {
  43. [super didReceiveMemoryWarning];
  44. // Dispose of any resources that can be recreated.
  45. }
  46. - (void)creatNavBar {
  47. self.title = @"发布技能";
  48. //
  49. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  50. btn.frame = CGRectMake(0, 0, 40, 40);
  51. [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal];
  52. btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
  53. [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  54. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  55. UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  56. // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整
  57. self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
  58. }
  59. - (void)backClick {
  60. [self.navigationController popViewControllerAnimated:YES];
  61. }
  62. - (void)creatSubViews {
  63. //
  64. UIScrollView *scrollView = [[UIScrollView alloc] init];
  65. scrollView.frame = self.view.bounds;
  66. scrollView.showsVerticalScrollIndicator = NO;
  67. scrollView.backgroundColor = [UIColor whiteColor];
  68. CGFloat height = ScreenHeight < 667 ? 697 : ScreenHeight;
  69. scrollView.contentSize = CGSizeMake(ScreenWidth, height);
  70. [self.view addSubview:scrollView];
  71. self.bgScrollView = scrollView;
  72. //
  73. UILabel *workTypeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 80, 15)];
  74. workTypeTitleLabel.text = @"工作类型:";
  75. workTypeTitleLabel.font = [UIFont systemFontOfSize:15];
  76. workTypeTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
  77. [scrollView addSubview:workTypeTitleLabel];
  78. UITextField *workTypeTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workTypeTitleLabel.frame), 20, ScreenWidth / 2, 15)];
  79. workTypeTF.placeholder = @"选择您的工作类型";
  80. workTypeTF.userInteractionEnabled = NO;
  81. workTypeTF.font = [UIFont systemFontOfSize:15];
  82. workTypeTF.textColor = RGBValueColor(0x333333, 1.0);
  83. [scrollView addSubview:workTypeTF];
  84. self.workTypeTF = workTypeTF;
  85. self.bgSkillView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(workTypeTitleLabel.frame) + 20, ScreenWidth, ((self.skillArray.count-1)/3+1)*50)];
  86. self.bgSkillView.backgroundColor = [UIColor whiteColor];
  87. [scrollView addSubview:self.bgSkillView];
  88. int width = (ScreenWidth-120)/3;
  89. for (int i=0; i<self.skillArray.count; i++) {
  90. int m = i % 3;
  91. int n = i / 3;
  92. UIButton *skillButton = [[UIButton alloc]initWithFrame:CGRectMake(20+(width+40)*m, 50*n, width, 35)];
  93. skillButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  94. [skillButton setTitle:[self.skillArray[i] objectForKey:@"name"] forState:UIControlStateNormal];
  95. [skillButton setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
  96. [skillButton setTitleColor:RGB(255, 255, 255) forState:UIControlStateSelected];
  97. [skillButton setBackgroundImage:[UIImage imageNamed:@"weixuanzhong"] forState:UIControlStateNormal];
  98. [skillButton setBackgroundImage:[UIImage imageNamed:@"xuanzhonghou"] forState:UIControlStateSelected];
  99. skillButton.layer.cornerRadius = 2;
  100. skillButton.tag = i;
  101. skillButton.titleLabel.font = [UIFont systemFontOfSize:14];
  102. [skillButton addTarget:self action:@selector(selectedAmountBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  103. [self.bgSkillView addSubview:skillButton];
  104. }
  105. scrollView.contentSize = CGSizeMake(ScreenWidth, self.bgSkillView.frame.size.height + 400);
  106. //
  107. UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bgSkillView.frame), ScreenWidth, 0.5)];
  108. upLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0);
  109. [scrollView addSubview:upLineView];
  110. UILabel *workPicTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(upLineView.frame) + 20, 80, 15)];
  111. workPicTitleLabel.text = @"工作照片:";
  112. workPicTitleLabel.font = [UIFont systemFontOfSize:15];
  113. workPicTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
  114. [scrollView addSubview:workPicTitleLabel];
  115. self.workPicView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workPicTitleLabel.frame), CGRectGetMaxY(upLineView.frame) + 20, 80, 80)];
  116. self.workPicView.image = [UIImage imageNamed:@"tianjiajineng"];
  117. [scrollView addSubview:self.workPicView];
  118. UIButton *addPicBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workPicTitleLabel.frame), CGRectGetMaxY(upLineView.frame) + 20, 80, 80)];
  119. addPicBtn.backgroundColor = [UIColor clearColor];
  120. [addPicBtn addTarget:self action:@selector(addPicBtnAction) forControlEvents:UIControlEventTouchUpInside];
  121. [scrollView addSubview:addPicBtn];
  122. UIView *downLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.workPicView.frame) + 20, ScreenWidth, 0.5)];
  123. downLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0);
  124. [scrollView addSubview:downLineView];
  125. //
  126. UILabel *priceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(downLineView.frame) + 20, 50, 15)];
  127. priceTitleLabel.text = @"价格:";
  128. priceTitleLabel.font = [UIFont systemFontOfSize:15];
  129. priceTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
  130. [scrollView addSubview:priceTitleLabel];
  131. UITextField *priceTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(priceTitleLabel.frame), CGRectGetMaxY(downLineView.frame) + 20, 100, 15)];
  132. priceTF.textAlignment = NSTextAlignmentLeft;
  133. priceTF.keyboardType = UIKeyboardTypeDecimalPad;
  134. priceTF.font = [UIFont systemFontOfSize:15];
  135. priceTF.textColor = RGBValueColor(0x333333, 1.0);
  136. priceTF.placeholder = @"请填写价格";
  137. priceTF.tag = 700;
  138. priceTF.delegate = self;
  139. [scrollView addSubview:priceTF];
  140. self.priceTF = priceTF;
  141. UILabel *unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.priceTF.frame), CGRectGetMaxY(downLineView.frame) + 20, 100, 15)];
  142. unitLabel.textAlignment = NSTextAlignmentLeft;
  143. unitLabel.text = @"元/小时";
  144. unitLabel.font = [UIFont systemFontOfSize:15];
  145. unitLabel.textColor = RGBValueColor(0x333333, 1.0);
  146. [scrollView addSubview:unitLabel];
  147. //
  148. UIButton *postBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  149. postBtn.frame = CGRectMake(20, CGRectGetMaxY(priceTitleLabel.frame) + 70, ScreenWidth - 40, 45);
  150. [postBtn setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:UIControlStateNormal];
  151. [postBtn setTitle:@"确认发布" forState:UIControlStateNormal];
  152. [postBtn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateNormal];
  153. [postBtn addTarget:self action:@selector(postBtnAction) forControlEvents:UIControlEventTouchUpInside];
  154. [scrollView addSubview:postBtn];
  155. //
  156. UILabel *instructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(postBtn.frame) + 16, ScreenWidth - 40, 12)];
  157. instructionsLabel.text = @"说明:请勿发布违反国家法规的内容";
  158. instructionsLabel.font = [UIFont systemFontOfSize:12];
  159. instructionsLabel.textColor = RGBValueColor(0x333333, 1.0);
  160. [scrollView addSubview:instructionsLabel];
  161. }
  162. #pragma mark -- 选中的工作方式
  163. - (void)selectedAmountBtnAction:(UIButton *)sender {
  164. skillPk = [self.skillArray[sender.tag] objectForKey:@"pk"];
  165. self.workTypeTF.text = sender.currentTitle;
  166. self.selectedWorkTypeBtn.selected = NO;
  167. sender.selected = !sender.selected;
  168. self.selectedWorkTypeBtn = sender;
  169. }
  170. #pragma mark -- 确认发布
  171. - (void)postBtnAction {
  172. NSLog(@"确认发布");
  173. if (self.workTypeTF.text.length == 0) {
  174. [MBProgressHUD showOnlyText:@"请选择工作类型" controller:self];
  175. return;
  176. }
  177. if (self.imageStr.length == 0) {
  178. [MBProgressHUD showOnlyText:@"请选择工作照片" controller:self];
  179. return;
  180. }
  181. if (self.priceTF.text.length == 0) {
  182. [MBProgressHUD showOnlyText:@"请填写价格" controller:self];
  183. return;
  184. }
  185. [self postDataFromNetworking];
  186. }
  187. #pragma mark -- 获取技能标签
  188. -(void)getSkillFromNetworking{
  189. // 获取技能标签
  190. [YanCNetWorkManager requestGETWithURLStr:Url_getSkill(PublicUrl) parameters:nil finish:^(id dataDic) {
  191. NSString *issuccess = dataDic[@"msg"];
  192. if ([issuccess isEqualToString:@"success"]) {
  193. self.skillArray = dataDic[@"data"];
  194. //设置子试图
  195. [self creatSubViews];
  196. } else {
  197. [MBProgressHUD hideHUDForView:self.view animated:YES];
  198. [MBProgressHUD showInfo:@"请求失败!"];
  199. }
  200. } enError:^(NSError *error) {
  201. [MBProgressHUD hideHUDForView:self.view animated:YES];
  202. }];
  203. }
  204. #pragma mark -- 发布技能
  205. - (void)postDataFromNetworking {
  206. ModelUser *modelUser = [ModelUser user];
  207. NSString *price = [NSString stringWithFormat:@"%@", self.priceTF.text];
  208. NSMutableDictionary *parameterDic = [NSMutableDictionary dictionaryWithCapacity:0];
  209. [parameterDic setObject:modelUser.modelpk forKey:@"modelpk"];
  210. [parameterDic setObject:self.imageStr forKey:@"skillphoto"]; //返回的url
  211. [parameterDic setObject:skillPk forKey:@"skillpk"];
  212. [parameterDic setObject:price forKey:@"price"];
  213. [parameterDic setObject:@"元/小时" forKey:@"unit"];
  214. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  215. [YanCNetWorkManager requestPostWithURLStr:Url_addskill(PublicUrl) parameters:parameterDic finish:^(id dataDic) {
  216. [MBProgressHUD hideHUDForView:self.view animated:YES];
  217. NSString *issuccess = dataDic[@"msg"];
  218. if ([issuccess isEqualToString:@"success"]) {
  219. [MBProgressHUD showInfo:@"发布成功!"];
  220. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  221. } else {
  222. [MBProgressHUD showInfo:@"请求失败!"];
  223. }
  224. } enError:^(NSError *error) {
  225. [MBProgressHUD hideHUDForView:self.view animated:YES];
  226. NSLog(@"error = %@", error);
  227. }];
  228. }
  229. #pragma mark -- 添加工作照
  230. - (void)addPicBtnAction {
  231. NSLog(@"添加工作照");
  232. [self changeHeaderPicture];
  233. }
  234. #pragma mark -- 获取图片
  235. #pragma mark - 更换头像的方法
  236. - (void)changeHeaderPicture {
  237. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
  238. // 设置按钮
  239. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册中选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  240. NSLog(@"选择相册");
  241. [self pickerPictureFromPhotoLibrary];
  242. }];
  243. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"使用相机拍摄" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  244. NSLog(@"请选择相机");
  245. [self pickerPictureFromCamera];
  246. }];
  247. UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
  248. [alertController addAction:action1];
  249. [alertController addAction:action2];
  250. [alertController addAction:action3];
  251. [self presentViewController:alertController animated:YES completion:nil];
  252. }
  253. // 从相册中选择图片
  254. - (void)pickerPictureFromPhotoLibrary {
  255. // UIImagePickerController 图片选择控制器
  256. UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
  257. // 设置图片选择控制器从哪种资源类型中获取图片
  258. [pickerVC setSourceType:(UIImagePickerControllerSourceTypePhotoLibrary)];
  259. // 是否允许编辑获取到的图片
  260. pickerVC.allowsEditing = YES;
  261. // 设置代理
  262. pickerVC.delegate = self;
  263. [self presentViewController:pickerVC animated:YES completion:nil];
  264. }
  265. // 从相机中获取图片
  266. - (void)pickerPictureFromCamera {
  267. // 1.先判断设备摄像头是否可用
  268. BOOL isCan = [UIImagePickerController isCameraDeviceAvailable:(UIImagePickerControllerCameraDeviceRear)];
  269. if (!isCan) { // 相机不可使用提前结束方法
  270. NSLog(@"设备不可用,提前结束");
  271. return;
  272. }
  273. // 初始化图片选择视图控制器
  274. UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
  275. // 设置获取资源的类型
  276. pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
  277. // 设置是否允许编辑图片
  278. pickerVC.allowsEditing = YES;
  279. // 设置代理
  280. pickerVC.delegate = self;
  281. [self presentViewController:pickerVC animated:YES completion:nil];
  282. }
  283. #pragma mark - UIImagePickerControllerDelegate的代理方法
  284. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
  285. // // info存储的是我们要的图片, 而图片是编辑后要得到的, 所以使用UIImagePickerControllerEditedImage
  286. //先要把头像上传,若上传成功了,就把这个图片给显示出来
  287. //先要把头像上传,若上传成功了,就把这个图片给显示出来
  288. [self dismissViewControllerAnimated:YES completion:^{
  289. NSString *mediaType = info[UIImagePickerControllerMediaType];
  290. if ([mediaType isEqualToString:@"public.image"]) {
  291. NSLog(@"image...");
  292. //获取图片原图
  293. UIImage *original = [info objectForKey:UIImagePickerControllerEditedImage];
  294. //设置压缩的image的尺寸
  295. CGSize imagesize = original.size;
  296. imagesize.width = 100;
  297. imagesize.height = 100;
  298. //对图片大小进行压缩
  299. UIImage *newImg = [self imageWithImage:original scaledToSize:imagesize];
  300. NSData *imageData = UIImageJPEGRepresentation(newImg,1);
  301. // UIImage *selectImg = [[UIImage alloc] init];
  302. UIImage *selectImg = [UIImage imageWithData:imageData];
  303. // NSLog(@"selectImg:%@",selectImg);
  304. self.workPicView.image = selectImg;
  305. [self postPicDataWithNetworkingWith:newImg];
  306. }
  307. }];
  308. }
  309. //用户取消退出picker时候调用
  310. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  311. NSLog(@"picker = %@",picker);
  312. [self dismissViewControllerAnimated:YES completion:^{
  313. }];
  314. }
  315. //对图片尺寸进行压缩--
  316. - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
  317. // Create a graphics image context
  318. UIGraphicsBeginImageContext(newSize);
  319. // Tell the old image to draw in this new context, with the desired
  320. // new size
  321. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  322. // Get the new image from the context
  323. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  324. // End the context
  325. UIGraphicsEndImageContext();
  326. // Return the new image.
  327. return newImage;
  328. }
  329. #pragma mark -- 上传图片
  330. - (void)postPicDataWithNetworkingWith:(UIImage *)image {
  331. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  332. [YanCNetWorkManager requestPostWithURLStr:Url_skillphoto(PublicUrl) fileData:image name:@"skillImage" fileName:@"111.png" mimeType:@"image/jpg/png" parameters:nil finish:^(id dataDic) {
  333. [MBProgressHUD hideHUDForView:self.view animated:YES];
  334. NSString *issuccess = dataDic[@"msg"];
  335. if ([issuccess isEqualToString:@"success"]) {
  336. self.imageStr = [NSString stringWithFormat:@"%@", dataDic[@"img"]];
  337. } else {
  338. [MBProgressHUD showInfo:@"请求失败!"];
  339. }
  340. } enError:^(NSError *error) {
  341. [MBProgressHUD hideHUDForView:self.view animated:YES];
  342. }];
  343. }
  344. /*
  345. {
  346. img = "upload/photo/20180720/A47B4B53E849FE2F92F90C93FAD4F1C4.jpg";
  347. msg = success;
  348. }
  349. */
  350. #pragma mark ----- UITextFieldDelegate
  351. - (void)textFieldDidEndEditing:(UITextField *)textField {
  352. NSLog(@"textField.text = %@", textField.text);
  353. }
  354. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  355. if (textField.tag == 700) {
  356. if(![self isValidAboutInputText:textField shouldChangeCharactersInRange:range replacementString:string decimalNumber:2]) {
  357. return NO;
  358. }
  359. }
  360. return YES;
  361. }
  362. //输入框中只能输入数字和小数点,且小数点只能输入一位,参数number 可以设置小数的位数,该函数在-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string调用;
  363. - (BOOL)isValidAboutInputText:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string decimalNumber:(NSInteger)number {
  364. NSScanner *scanner = [NSScanner scannerWithString:string];
  365. NSCharacterSet *numbers;
  366. NSRange pointRange = [textField.text rangeOfString:@"."];
  367. if ( (pointRange.length > 0) && (pointRange.location < range.location || pointRange.location > range.location + range.length) ){
  368. numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  369. }else{
  370. numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
  371. }
  372. if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] ){
  373. return NO;
  374. }
  375. short remain = number; //保留 number位小数
  376. NSString *tempStr = [textField.text stringByAppendingString:string];
  377. NSUInteger strlen = [tempStr length];
  378. if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
  379. if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
  380. return NO;
  381. }
  382. if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
  383. return NO;
  384. }
  385. }
  386. NSRange zeroRange = [textField.text rangeOfString:@"0"];
  387. if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
  388. if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
  389. textField.text = string;
  390. return NO;
  391. }else{
  392. if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
  393. if([string isEqualToString:@"0"]){
  394. return NO;
  395. }
  396. }
  397. }
  398. }
  399. NSString *buffer;
  400. if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) ){
  401. return NO;
  402. }else{
  403. return YES;
  404. }
  405. }
  406. @end