| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- //
- // CreateLiveBodyView.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/30.
- //
- #import "CreateLiveBodyView.h"
- #import <UIView+ExtensionForDotLine.h>
- @interface CreateLiveBodyView ()<UITextFieldDelegate,UITextViewDelegate>
- @property (weak, nonatomic) IBOutlet UITextField *titleField;
- @property (weak, nonatomic) IBOutlet UITextView *inputView;
- @property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
- @property (weak, nonatomic) IBOutlet UILabel *countLabel;
- @property (weak, nonatomic) IBOutlet UIView *buttonBgView;
- @property (nonatomic, copy) CreateLiveCallback callback;
- @property (nonatomic, copy) ChooseLiveCoverCallback chooseCoverBlock;
- @end
- @implementation CreateLiveBodyView
- - (void)awakeFromNib {
- [super awakeFromNib];
- self.titleField.delegate = self;
- self.titleField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入直播标题" attributes:@{NSForegroundColorAttributeName:HexRGB(0xc1c1c1)}];
- self.inputView.delegate = self;
- [self.buttonBgView drawBoardDottedLine:1.0f length:4 space:4 cornerRadius:10.0f lineColor:HexRGB(0xe3e3e3)];
- }
- + (instancetype)shareInstance {
- CreateLiveBodyView *view = [[[NSBundle mainBundle] loadNibNamed:@"CreateLiveBodyView" owner:nil options:nil] firstObject];
- return view;
- }
- - (void)createSureCallback:(CreateLiveCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (void)chooseCoverCallback:(ChooseLiveCoverCallback)callback {
- if (callback) {
- self.chooseCoverBlock = callback;
- }
- }
- - (IBAction)chooseCover:(id)sender {
- if (self.chooseCoverBlock) {
- self.chooseCoverBlock();
- }
- }
- - (IBAction)sureAction:(id)sender {
- [self endEditing:YES];
- if ([NSString isEmptyString:self.titleField.text]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请输入直播标题"];
- return;
- }
- if ([NSString isEmptyString:self.inputView.text]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请输入直播内容"];
- return;
- }
- if ([self.timeLabel.text isEqualToString:@"请选择直播时长"]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请选择直播时长"];
- return;
- }
- if (self.callback) {
- self.callback(NO,self.titleField.text, self.inputView.text);
- }
- }
- - (IBAction)chooseTime:(id)sender {
- [self endEditing:YES];
- if (self.callback) {
- self.callback(YES, @"", @"");
- }
- }
- #pragma mark ---- delegate
- - (void)textViewDidBeginEditing:(UITextView *)textView {
- self.tipsLabel.hidden = YES;
- }
- - (void)textViewDidEndEditing:(UITextView *)textView {
- if ([NSString isEmptyString:textView.text]) {
- self.tipsLabel.hidden = NO;
- }
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- UITextRange *markedTextRange = textView.markedTextRange;
- if (markedTextRange) {
- // 当前处于拼音输入状态,暂不更新 attributedText
- return YES;
- }
- NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
- if (newText.length > 200) {
- newText = [newText substringWithRange:NSMakeRange(0, 200)];
- textView.text = newText;
- }
- return YES;
- }
- - (void)updateTextViewLineHeight:(UITextView *)textView {
- UITextRange *markedTextRange = textView.markedTextRange;
- if (markedTextRange) {
- // 当前处于拼音输入状态,暂不更新 attributedText
- return;
- }
- [UIView setAnimationsEnabled:YES];
- }
- - (void)textViewDidChange:(UITextView *)textView {
- // 获取当前高亮的部分(如果正在拼音输入状态)
- UITextRange *selectedRange = [textView markedTextRange];
- UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
-
- // 如果没有高亮选择的文本,说明不是拼音输入状态
- if (!position) {
- // 获取当前textView的内容
- NSString *currentText = textView.text;
-
- // 如果文本超出最大长度,进行截取
- if (currentText.length > 200) {
- NSString *limitedText = [currentText substringToIndex:200];
- textView.text = limitedText;
- }
- self.countLabel.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
- }
- [self updateTextViewLineHeight:textView];
- }
- - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
- [self endEditing:YES];
- return YES;
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- [self endEditing:YES];
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- if ([string isEqualToString:@"\n"]) {
- [self endEditing:YES];
- return YES;
- }
- return YES;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|