YBSetChargeView.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // YBSetChargeView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/27.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBSetChargeView.h"
  9. @interface YBSetChargeView()<UITextFieldDelegate>
  10. @end
  11. @implementation YBSetChargeView
  12. - (void)awakeFromNib {
  13. [super awakeFromNib];
  14. UIImageView *rightView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 12.5, 40, 15)];
  15. rightView.contentMode = UIViewContentModeScaleAspectFit;
  16. rightView.image = [UIImage imageNamed:@"礼物-金币"];
  17. _chargeTF.rightView = rightView;
  18. _chargeTF.rightViewMode = UITextFieldViewModeAlways;
  19. _titleL.text = YZMsg(@"请输入收费金额");
  20. _tipsL.text = YZMsg(@"*收取金额不可为小数");
  21. [_sureBtn setTitle:YZMsg(@"确认") forState:0];
  22. }
  23. +(instancetype)showInputView:(NSString *)price complete:(YBSetChargeBlock)complete;{
  24. YBSetChargeView *cV = [[[NSBundle mainBundle]loadNibNamed:@"YBSetChargeView" owner:nil options:nil] objectAtIndex:0];
  25. if (complete) {
  26. cV.chargeEvent = ^(NSString * _Nonnull priceStr) {
  27. complete(priceStr);
  28. };
  29. }
  30. [cV setUpView:price];
  31. return cV;
  32. }
  33. -(void)setUpView:(NSString *)priceStr {
  34. self.frame = [UIScreen mainScreen].bounds;
  35. _chargeTF.tintColor = RGB_COLOR(@"#323232", 1);
  36. _chargeTF.keyboardType = UIKeyboardTypeNumberPad;
  37. _chargeTF.delegate = self;
  38. _chargeTF.placeCol = RGB_COLOR(@"#969696", 1);
  39. _chargeTF.placeholder = YZMsg(@"请输入收取金额");
  40. _chargeTF.text = priceStr;
  41. _chargeTF.textColor = RGB_COLOR(@"#323232", 1);
  42. _chargeTF.font = SYS_Font(13);
  43. [[UIApplication sharedApplication].delegate.window addSubview:self];
  44. [UIView animateWithDuration:0.3 animations:^{
  45. } completion:^(BOOL finished) {
  46. self.backgroundColor = RGB_COLOR(@"#000000", 0.4);
  47. }];
  48. }
  49. -(void)dismiss {
  50. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  51. [self removeFromSuperview];
  52. }
  53. - (IBAction)clickCancleBtn:(id)sender {
  54. [self dismiss];
  55. }
  56. - (IBAction)clickSureBtn:(id)sender {
  57. if (self.chargeEvent) {
  58. self.chargeEvent(_chargeTF.text);
  59. }
  60. [self dismiss];
  61. }
  62. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  63. if (textField == _chargeTF) {
  64. //新输入的
  65. if (string.length == 0) {
  66. return YES;
  67. }
  68. NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
  69. NSString *regex = @"^[1-9]\\d*|0$";
  70. return [self isValid:checkStr withRegex:regex];
  71. }
  72. return YES;
  73. }
  74. //检测改变过的文本是否匹配正则表达式,如果匹配表示可以键入,否则不能键入
  75. - (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex {
  76. NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
  77. return [predicte evaluateWithObject:checkStr];
  78. }
  79. @end