CreateLiveBodyView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // CreateLiveBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/30.
  6. //
  7. #import "CreateLiveBodyView.h"
  8. #import <UIView+ExtensionForDotLine.h>
  9. @interface CreateLiveBodyView ()<UITextFieldDelegate,UITextViewDelegate>
  10. @property (weak, nonatomic) IBOutlet UITextField *titleField;
  11. @property (weak, nonatomic) IBOutlet UITextView *inputView;
  12. @property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
  13. @property (weak, nonatomic) IBOutlet UILabel *countLabel;
  14. @property (weak, nonatomic) IBOutlet UIView *buttonBgView;
  15. @property (nonatomic, copy) CreateLiveCallback callback;
  16. @property (nonatomic, copy) ChooseLiveCoverCallback chooseCoverBlock;
  17. @end
  18. @implementation CreateLiveBodyView
  19. - (void)awakeFromNib {
  20. [super awakeFromNib];
  21. self.titleField.delegate = self;
  22. self.titleField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入直播标题" attributes:@{NSForegroundColorAttributeName:HexRGB(0xc1c1c1)}];
  23. self.inputView.delegate = self;
  24. [self.buttonBgView drawBoardDottedLine:1.0f length:4 space:4 cornerRadius:10.0f lineColor:HexRGB(0xe3e3e3)];
  25. }
  26. + (instancetype)shareInstance {
  27. CreateLiveBodyView *view = [[[NSBundle mainBundle] loadNibNamed:@"CreateLiveBodyView" owner:nil options:nil] firstObject];
  28. return view;
  29. }
  30. - (void)createSureCallback:(CreateLiveCallback)callback {
  31. if (callback) {
  32. self.callback = callback;
  33. }
  34. }
  35. - (void)chooseCoverCallback:(ChooseLiveCoverCallback)callback {
  36. if (callback) {
  37. self.chooseCoverBlock = callback;
  38. }
  39. }
  40. - (IBAction)chooseCover:(id)sender {
  41. if (self.chooseCoverBlock) {
  42. self.chooseCoverBlock();
  43. }
  44. }
  45. - (IBAction)sureAction:(id)sender {
  46. [self endEditing:YES];
  47. if ([NSString isEmptyString:self.titleField.text]) {
  48. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请输入直播标题"];
  49. return;
  50. }
  51. if ([NSString isEmptyString:self.inputView.text]) {
  52. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请输入直播内容"];
  53. return;
  54. }
  55. if ([self.timeLabel.text isEqualToString:@"请选择直播时长"]) {
  56. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请选择直播时长"];
  57. return;
  58. }
  59. if (self.callback) {
  60. self.callback(NO,self.titleField.text, self.inputView.text);
  61. }
  62. }
  63. - (IBAction)chooseTime:(id)sender {
  64. [self endEditing:YES];
  65. if (self.callback) {
  66. self.callback(YES, @"", @"");
  67. }
  68. }
  69. #pragma mark ---- delegate
  70. - (void)textViewDidBeginEditing:(UITextView *)textView {
  71. self.tipsLabel.hidden = YES;
  72. }
  73. - (void)textViewDidEndEditing:(UITextView *)textView {
  74. if ([NSString isEmptyString:textView.text]) {
  75. self.tipsLabel.hidden = NO;
  76. }
  77. }
  78. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  79. UITextRange *markedTextRange = textView.markedTextRange;
  80. if (markedTextRange) {
  81. // 当前处于拼音输入状态,暂不更新 attributedText
  82. return YES;
  83. }
  84. NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
  85. if (newText.length > 200) {
  86. newText = [newText substringWithRange:NSMakeRange(0, 200)];
  87. textView.text = newText;
  88. }
  89. return YES;
  90. }
  91. - (void)updateTextViewLineHeight:(UITextView *)textView {
  92. UITextRange *markedTextRange = textView.markedTextRange;
  93. if (markedTextRange) {
  94. // 当前处于拼音输入状态,暂不更新 attributedText
  95. return;
  96. }
  97. [UIView setAnimationsEnabled:YES];
  98. }
  99. - (void)textViewDidChange:(UITextView *)textView {
  100. // 获取当前高亮的部分(如果正在拼音输入状态)
  101. UITextRange *selectedRange = [textView markedTextRange];
  102. UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
  103. // 如果没有高亮选择的文本,说明不是拼音输入状态
  104. if (!position) {
  105. // 获取当前textView的内容
  106. NSString *currentText = textView.text;
  107. // 如果文本超出最大长度,进行截取
  108. if (currentText.length > 200) {
  109. NSString *limitedText = [currentText substringToIndex:200];
  110. textView.text = limitedText;
  111. }
  112. self.countLabel.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
  113. }
  114. [self updateTextViewLineHeight:textView];
  115. }
  116. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  117. [self endEditing:YES];
  118. return YES;
  119. }
  120. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  121. [self endEditing:YES];
  122. }
  123. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  124. if ([string isEqualToString:@"\n"]) {
  125. [self endEditing:YES];
  126. return YES;
  127. }
  128. return YES;
  129. }
  130. /*
  131. // Only override drawRect: if you perform custom drawing.
  132. // An empty implementation adversely affects performance during animation.
  133. - (void)drawRect:(CGRect)rect {
  134. // Drawing code
  135. }
  136. */
  137. @end