PostSkillsController.m 22 KB

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