CreateLiveBodyView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // CreateLiveBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/30.
  6. //
  7. #import "CreateLiveBodyView.h"
  8. @interface CreateLiveBodyView ()<UITextFieldDelegate,UITextViewDelegate>
  9. @property (weak, nonatomic) IBOutlet UITextField *titleField;
  10. @property (weak, nonatomic) IBOutlet UITextView *inputView;
  11. @property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
  12. @property (weak, nonatomic) IBOutlet UILabel *countLabel;
  13. @property (nonatomic, copy) CreateLiveCallback callback;
  14. @end
  15. @implementation CreateLiveBodyView
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. self.titleField.delegate = self;
  19. self.titleField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入直播标题" attributes:@{NSForegroundColorAttributeName:HexRGB(0xc1c1c1)}];
  20. self.inputView.delegate = self;
  21. }
  22. + (instancetype)shareInstance {
  23. CreateLiveBodyView *view = [[[NSBundle mainBundle] loadNibNamed:@"CreateLiveBodyView" owner:nil options:nil] firstObject];
  24. return view;
  25. }
  26. - (void)createSureCallback:(CreateLiveCallback)callback {
  27. if (callback) {
  28. self.callback = callback;
  29. }
  30. }
  31. - (IBAction)sureAction:(id)sender {
  32. [self endEditing:YES];
  33. if ([NSString isEmptyString:self.titleField.text]) {
  34. [self MBPShow:@"请输入直播标题"];
  35. return;
  36. }
  37. if ([NSString isEmptyString:self.inputView.text]) {
  38. [self MBPShow:@"请输入直播内容"];
  39. return;
  40. }
  41. if ([self.timeLabel.text isEqualToString:@"请选择直播时长"]) {
  42. [self MBPShow:@"请选择直播时长"];
  43. return;
  44. }
  45. if (self.callback) {
  46. self.callback(NO,self.titleField.text, self.inputView.text);
  47. }
  48. }
  49. - (IBAction)chooseTime:(id)sender {
  50. [self endEditing:YES];
  51. if (self.callback) {
  52. self.callback(YES, @"", @"");
  53. }
  54. }
  55. #pragma mark ---- delegate
  56. - (void)textViewDidBeginEditing:(UITextView *)textView {
  57. self.tipsLabel.hidden = YES;
  58. }
  59. - (void)textViewDidEndEditing:(UITextView *)textView {
  60. if ([NSString isEmptyString:textView.text]) {
  61. self.tipsLabel.hidden = NO;
  62. }
  63. }
  64. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  65. if ([text isEqualToString:@"\n"]) {
  66. [self endEditing:YES];
  67. return YES;
  68. }
  69. // 长度判断
  70. // 输入控制
  71. NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
  72. if (newString.length > 200) {
  73. return NO;
  74. }
  75. self.countLabel.text = [NSString stringWithFormat:@"%zd/200",newString.length];
  76. return YES;
  77. }
  78. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  79. [self endEditing:YES];
  80. return YES;
  81. }
  82. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  83. [self endEditing:YES];
  84. }
  85. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  86. if ([string isEqualToString:@"\n"]) {
  87. [self endEditing:YES];
  88. return YES;
  89. }
  90. return YES;
  91. }
  92. /*
  93. // Only override drawRect: if you perform custom drawing.
  94. // An empty implementation adversely affects performance during animation.
  95. - (void)drawRect:(CGRect)rect {
  96. // Drawing code
  97. }
  98. */
  99. @end